Classes and object orientation (1)
theory
Relationship between class and object:
1. Object oriented is a programming idea
2. Class is a kind of grammar
3. Grammar is the basis of realizing programming ideas
class
Definition: a collection of objects with the same attributes (data members) and behaviors (functions), such as:
Human beings are a collection of people, and I am the specific individual of this class, which is called the object
Class is a kind of syntax, the keyword is class, and the struct of C language is a special case of class
Member call:
1. Ordinary object call in stack area
2. Heap pointer object call
3. All members of a class (except some special static) must be accessed through objects
4. Use of classes and objects:
#include<iostream> using namespace std; class Cpeople { public: //Access modifier int a; void fun() { cout << "fun:" << a << endl; } }; int main() { /*There are two ways to declare objects: stack ordinary variables and heap pointer variables*/ Cpeople we;//The declared object we (in the stack area) is an object of the class Cpeople //The pointer variable class space has not been applied for. It can only be calculated by applying for a heap space and Cpeole space Cpeople *we1 = new Cpeople;//Declare a pointer object //Call stack area ordinary member we.a = 12; we.fun(); //Call heap pointer member we1->a = 14; we1->fun(); system("pause"); return 0; } //Objects such as we can declare countless classes, which are a collection of public properties of all objects
Access modifier:
Scope: from the writing position to the end of the next access modifier, or to the end of the curly bracket at the end of the class
Function: it can be used as a tool for classifying members in a class (classifying code blocks of certain common functions in a class)
Multiple access modifiers can be written in a class:
class Cpeople { int a; public: void we() { cout << "we" << endl; } private: void fun3() { we(); } };
1.public
(1) Make members of the class visible to the outside world
(2) Visible to other classes
(3) Visible to other functions
(4) If you don't write, an error will be reported: you can't access private members
class Cpeople { public: int a; void we() { cout << "we" << endl; } }; //Visible to other classes class Cperson { public: Cpeople westrong1; void we2() { //Visible to other functions Cpeople westrong2; cout << "we1" << endl; } };
2. Private (compared with public)
(1) When there is no access modifier, the default is the private modifier
Special: struct defaults to public
struct we //Plus, it becomes private { private: int a = 12; };
(2) Visible within class only
3.protected
Function: visible only within the class and subclasses of the class
class westrong//Declaration class { protected: int a = 12; void we() { cout << a << endl; } }; //westrong subclass class wq : public westrong { void fun() { we(); } };
4. Friends
Key words: friend
Function: make private members visible to the specified item
For example, for a private member, after a function declares its friend function in the class, the friend function can directly use the private member
Features: not affected by access modifier; Can have multiple friends;
private: //useless friend void fun1();
It destroys the encapsulation of the class. It's best not to use it. How to access the private?
Here is the interface function:
//Interface function friends suggest not to use it, just use interface function public: int Get()//Get value { return age; } void Set()//assignment { age = 13; } //Call interface function Cstu we1; we1.Set(); int a = we1.Get(); //There is a return value that can be followed by a variable cout << a << endl;
(1) If a friend function is declared in a class, the function can use any member in the class
class Cstu { private: int age = 12; void fun() { cout << age << endl; } friend void fun1(); //Declare fun1() as a friend function. Private classes can be used in fun1 friend int main(); //So is the main function friend class Cteach; //The same is true for friend classes };
(2) Members inherit (subclasses) by using the access of friends
(3) Total code of public class calling private class members through friends:
#include<iostream> using namespace std; class Cstu { private: int age = 12; void fun() { cout << age << endl; } friend void fun1(); //Declare fun() as a friend function friend int main(); //So is the main function friend class Cteach; //The same is true for friend classes }; class Cteach { public: Cstu we; void fun2() { we.fun(); } }; void fun1() { Cstu we; //Declaration object we.fun(); //Call fun(); } int main() { Cteach Teach; Teach.fun2(); system("pause"); return 0; }