C++ Class and Object Foundation

  • 2020-06-23 01:32:39
  • OfStack

A class is a template for creating objects. A class can create multiple objects. Each object is a variable of class type. The process of creating an object is also called class instantiation. Each object is a concrete instance of a class (Instance), with member variables and member functions of the class.

1. Class definition

1 simple class definition:


class Student{
public:
 // Member variables 
 char *name;
 int age;
 float score;

 // A member function 
 void say(){
  cout<<name<<" The age is "<<age<<" , the result is "<<score<<endl;
 }
};

class is a new keyword added to C++ to define classes. Student is the name of the class; Class names are capitalized to distinguish them from other identifiers. Inside {} are the member variables and member functions contained in the class, collectively referred to as the members of the class (Member); The parts surrounded by {} are sometimes called class bodies, similar to the concept of function bodies.

Note: There is a semicolon at the end of the class definition; , which is part 1 of the class definition, means that the class definition is over and cannot be omitted.

The class is just a template (Template) that takes up no memory when compiled, so you cannot initialize member variables when defining the class because there is no place to store the data. The member variable is allocated memory only after the object is created, at which point the value can be assigned.

2. Create an object and access the members of the class

Objects are created as follows:


#include <iostream>
using namespace std;

// Classes are usually defined outside functions 
class Student{
public:
 // Class contains variables 
 char *name;
 int age;
 float score;
 // Class contains functions 
 void say(){
  cout<<name<<" The age is "<<age<<" , the result is "<<score<<endl;
 }
};

int main(){
 // Create an object 
 Student stu;
 stu.name = " Xiao Ming ";
 stu.age = 15;
 stu.score = 92.5f;
 stu.say();

 return 0;
}

Operation results:

[

Xiao Ming's age is 15 and his score is 92.5

]

stu is an object that occupies memory space, so its member variables can be assigned or read. Classes are usually defined outside functions, but they can also be defined inside functions, but are rarely used in this way.

3. Use object Pointers

The classic Pointers in C are still widely used in C++, especially Pointers to objects, without which some functionality cannot be achieved.

The object stu created in the above code allocates memory on the stack and needs to be used & Get its address, for example:

[

Student stu;
Student *pStu = & stu;

]

pStu is a pointer to data of type Student, that is, objects created through Student.

Similarly, you can create objects in the heap area using the new keyword:

[

Student *pStu = new Student;

]

Such as:


#include <iostream>

using namespace std;

class Student {
public:
 char *name;
 int age;
 float score;

 void say() {
  cout << name << ",age is " << age << ",score is " << score << endl;
 }
};


int main()
{
// Student stu;
// stu.name = "JACK";
// stu.age = 15;
// stu.score = 92.5f;
// stu.say();

// Student stu;    //  Create objects in the stack area 
// Student *pStu = &stu;

 Student *pStu = new Student; //  Create an object in the heap area 
 pStu ->name = "Jack";
 pStu -> age = 10;
 pStu -> score = 92.5f;
 pStu -> say();

 delete pStu;     //  Delete the object 
 return 0;
}

Note: objects created on the stack have a name, such as stu, so pointing to it is not necessary. But the object created with new is not the same. It allocates memory on the heap and has no name. It only gets a pointer to it.

In other words, objects created on the heap using new are anonymous and cannot be used directly, but must have a pointer to it and access its member variables or member functions with a pointer.

Stack memory is automatically managed by the program, and delete cannot be used to delete objects created on the stack. Heap memory is managed by the programmer, and objects can be removed by delete after they are used. In real development, new and delete often come in pairs to ensure that objects that are no longer in use are deleted in a timely manner and to prevent a build-up of unused memory.

Here's what others have added

C++ Learn basic knowledge points and objects

1. How to define a class?

[

class Tv
{
public:
void change_vol();
void power();
//private:
int type;
char name[20];
}

]

2. Instantiation of objects

Definition: The process of generating multiple objects from one class in IDE.

Format: Class name, object name;
Example: Tv s;

Two ways:
ps: Stack space: holds local variables, automatic variables, functional parameters.
Features are first in, first out, memory managed by the system.
Heap space: The space applied by malloc, new, and other space allocation functions
Features advanced late out, managed by the user (release)

[

Instantiate objects from the stack: b. Instantiate objects from the heap:
int main() int main()
{ {
Tv tv; Tv *p = new tv; // Request memory from heap, use up and free
Tv tv[20]; Tv *q = new tv[20];
...... ......
return 0; // The stack space is released by the system delelte p; // Frees the memory space that the pointer points to
} delete []q;
p = NULL; // Point pointer to null
q = NULL;
return 0;
}

]

3. Access to object members

a. Access to object members in the stack

[

int main()
{Tv tv;
tv.type = 0;
tv.change_vol();
return 0;
}

]

b. Access objects in the heap

[

Single object: Array of objects:
int main(void) int main(void)
{ {
Tv *p = new tv; *p = new tv[5];
p- > type = 0; for (int i = 0; i < 5; i++)
p- > change_vol(); {
delete p; // When finished, free memory p[i]- > type = 0;
p = NULL; // Point pointer P to null p[i]- > change_vol();
return 0; }
} delete []p;
p = NULL;
return 0;
}

]

About the storage of classes in memory: When a class is defined, it does not allocate memory space on the heap or stack, only when it instantiates an object, the object's member variables will allocate memory space on the stack, and the member functions are still in the code segment. When multiple objects are instantiated from the same object, the member variable of each object allots different memory space on the stack, but multiple objects share a member function of a code segment through the corresponding interface.


Related articles: