Define abstract classes
class Person { public: virtual void born() = 0; };
Inheritance of classes
class Man :public Person { public: void born() { cout << "Born a man" << endl; } };
Use the new keyword to instantiate an object, and only pointer variables can be used to receive it
Therefore, when instantiating an object, as a parameter and return value of a function, you must use a pointer to use both
Person* generatePersion(Person* person1) { Person* person = new Man(); retrun person; }
Use ordinary object properties and methods to reference, but use the object represented by pointers to use ->
Student stu1("Zhang San",18,"Beijing"); // Use variables to instantiate objects directlyStudent *stu2 = new Student("Li Si",20,"Shanghai"); // Instantiate the object through a pointer(); stu2->study();
When defining a class, the attribute needs to assign an initial value.
class StudentId { private: static StudentId* si; // Must be modified with static static string id; // Must be modified with static}; string StudentId::id = "20200001"; StudentId* StudentId::si = NULL;
Pre-declaration of class
Before using this class, you must define this class, otherwise the compiler will not know that there is this class.
Therefore, when two classes are nested together, an error may be reported. The solution is to use the predeclaration
If another class related method is used during the implementation of the class method, another class related method is also used.
Then it is best to write the definition and implementation of the class method separately.
class AbstractChatroom; // Predeclaration of class class Member{ protected: AbstractChatroom *chatroom; // You must define it before use void setChatroom(AbstractChatroom *chatroom) { // Class methods can be implemented when class definition this->chatroom = chatroom; } void chatroom_play(); // When a method needs to be used in the predeclaration class, it can only be defined first and then implemented}; class AbstractChatroom{ // Specific definition of classpublic: Member member; // Nesting of classes virtual void sendText(string from,string to,string message) = 0; void play(){ cout << "Play in the chat room!" << enld; } }; void Member::chatroom_play(){ // Specific implementation of class methods chatroom -> play(); }
Use of namespaces
#include <iostream> using namespace std; namespace my_namespace{ // Define the namespace class Student{ // Objects in the namespacepublic: string name; int age; string home; Student(string name, int age, string home); string getName(); int getAge(); string getHome(); void setName(string name); void setAge(int age); void setHome(string home); }; Student::Student(string name, int age, string home){ this -> name = name; this -> age = age; this -> home = home; } string Student::getName(){ return name; } int Student::getAge(){ return age; } string Student::getHome(){ return home; } void Student::setName(string name){ this -> name = name; } void Student::setAge(int age){ this -> age = age; } void Student::setHome(string home){ this -> home = home; } } // Use namespace, method 1using namespace my_namespace; int main(){ Student *stu1 = new Student("Zhang San", 18, "Beijing"); cout << stu1 -> getName() << endl; } // Use namespace, method 2int main(){ my_namespace::Student *stu1 = new my_namespace::Student("Zhang San", 18, "Beijing"); cout << stu1 -> getName() << endl; }
Summarize
This is the article about some issues in C++ objects and inheritance. For more related C++ objects and inheritance content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!