C++ Initial Stage - Classes and Objects

Catalog

Class Definition

2. Access qualifiers and encapsulation of classes

3. Scope of Classes

Fourth, instantiation of classes

Five, class object size  

Six, this pointer

C focuses on the process, analyses the steps to solve the problem, and gradually solves the problem through function calls.

C++ focuses on objects, splitting one thing into different objects and exchanging them between them.

Class Definition

         In C, only variables can be defined in the structure, but in C++, not only variables but also functions can be defined in the structure. In C++, class is preferred as a substitute;

//Variables and functions can be defined within a struct
struct S
{
	void fun()
	{
		cout << "class" << endl;
	}
	char _name[20];
	int _age[3];
};
int main()
{
	S s;
	s.fun();
	return 0;
}

         Classes are the core feature of C++ and are used to specify the form of objects (i.e. describing objects), often referred to as user-defined types. Defining a class is essentially a blueprint for defining a data type;

Class Definition:

  • Class (class keyword) + classname (class name) + {} (class body) +;
  • Elements in a class are called members of the class, data in the class - class properties, and functions in the class - class methods / member functions.

There are two ways to define a class:

  • Declarations and definitions are all placed in the class body, and it is important to note that member functions, such as those defined in a class, may be handled by the compiler as inline functions.
  • Class declarations are placed in.h files and class definitions in.cpp files, which is generally the case;

Note:

  • The definition of member function, that is, the implementation of function;
  • The definition of member variables, that is, open intervals;
//Class declarations and definitions are all placed in the class body
//Compilers may treat member functions as inline functions
class Person
{
//Access modifier: private/public/protected
public: 
    //Method or member function
	void showInfo()
	{
		cout << _name << "/" << _sex << "/" << _age << endl;
	}
//Access modifier: private/public/protected
public:
    //Member variables
	char* _name;
	char* _sex;
	int _age;
}; //End of Number
//.h declaration
class Person
{
public:
	void showInfo();
public:
	char* _name;
	char* _sex;
	int _age;
};

//.cpp Definition
void Person::showInfo()
{
	cout << _name << "/" << _sex << "/" << _age << endl;
}

2. Access qualifiers and encapsulation of classes

         C++ implements encapsulation by combining the properties and methods of objects with classes and selectively providing their interfaces to external users through access privileges.

Access Qualifier

  • public-decorated members that are directly accessible outside the class;
  • protected, private ly modified members are not directly accessible outside the class;
  • Access scope starts from where the access qualifier appears until the next access qualifier appears.
  • class has private access by default and struct is public (C compatible);

Note: Access qualifiers are only useful at compilation time. When data is mapped to memory, there is no difference in access qualifiers.

  Differences between struct and class in C++:

  • C++ needs to be C-compatible, so struct in C++ can be used as a structure, and struct can also be used to define classes.
  • Like class definition classes, struct has public access by default and class is private;

encapsulation  

There are three main object-oriented features: encapsulation, inheritance, polymorphism, abstraction, reflection (java), etc.

Encapsulation: Organically combine data and methods, hide object properties and implementation details, and only expose interfaces to interact with objects; Encapsulation is essentially management.

3. Scope of Classes

  • A class defines a new scope in which all members of the class are within.
  • To define a member outside of a class, a scope resolver is used:, to indicate the class domain to which the member belongs;
//statement
class Person
{
public:
	void showInfo();
public:
	char _name[20];
	char _sex[3];
	int _age;
};

//Extraclass Definition
void Person::showInfo()
{
	cout << _name << "/" << _sex << "/" << _age << endl;
}

Fourth, instantiation of classes

The process of creating objects with class types is called instantiation of classes.

  • Classes are like models that limit their members, and defining a class does not allocate actual memory space for storage;
  • A class can instantiate multiple objects, and the instantiated objects occupy the actual physical space.
  • Classes are like real drawings, and class instantiation is built using drawings.
//Class Instantiation
Person man;
Person woman;

Five, class object size  

Class size

  • That is, the sum of "member variables" in this class;
  • Memory alignment is required;
  • Empty class, more special, the compiler will give a byte to uniquely identify this class, this byte does not store valid data;

How class objects are stored

  • Objects contain all class members. Object member variables are different, but they call the same function. Stored in this way, each object will save a code, wasting space;
  • Only member variables are saved, and member functions are stored in public code snippets.

Note: Alignment rules exist within structures

  • The first member offset is 0;
  • Other member variables should be aligned to an address multiple of the alignment number;
  • The total size of the structure, an integer multiple of the maximum alignment;
  • For example, a nested structure is aligned to an integer multiple of its maximum alignment number and the total size of the structure is an integer multiple of its maximum alignment number (including the alignment number of nested structures).
  • Alignment = Compiler default alignment number and smaller value of member size, vs defaults to 8;

Six, this pointer

  • In member function bodies, there is no distinction between different objects; When different objects call member functions, C++ solves this problem by introducing this pointer.
  • That is, the C++ compiler adds a hidden pointer parameter to each "non-static member function" that points to the current object;
  • In a function body, the operation of all member variables is accessed through this pointer; Only all operations are hidden from the user, no user transfer is required, and the compiler completes automatically;

The characteristics of this pointer

  • The type of this pointer - type* (const cannot be modified);
  • It can only be used inside a "member function";
  • The this pointer is essentially a parameter of a member function. When an object calls a member function, it passes the object address as an argument to the this parameter, which is stored in the stack frame.
  • The this pointer is the first implicit pointer parameter of a member function. The vs compiler usually passes it automatically through an ecx register without user transfer.
//Define a class
class Date
{
public:
	void Display()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	void SetDate(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
        Display();
	}
private:
	int _year;
	int _month;
	int _day;
};

//Actual member function
void Display(Date* this)
{
	cout << this->_year << "/" << this->_month << "/" << this->_day << endl;
}
void SetDate(Date* this, int year, int month, int day)
{
	this->_year = year;
	this->_month = month;
	this->_day = day;
    this->Display();
}
//This program runs normally
//Because the member function address exists in the common code snippet
//The function is called without accessing the space pointed to by p, and there is no dereference of a null pointer
//Only p is passed to the invisible this pointer, and there is no unreferenced this pointer inside the show function
class A
{
public:
    void show()
    {
        cout << _a << endl;
    }
private:
    int _a;
};
int main()
{
    A* p = nullptr;
    p->show();
    return 0;
}

Tags: C++ Programming

Posted on Fri, 22 Oct 2021 12:42:11 -0400 by superpimp