1.构造器与结构体的区别:构造器多支持方法。其作用有申请内存,初始化变量。
在c++中构造器没有返回值:语法Class Name();
2.析构器的作用:释放内存。
在c++中析构器没有返回值:语法~ClassName();
class Animal { public: std::string mouth; std::string name; void eat(); void sleep(); void drool(); Animal(std::string theName); }; class Pig:public Animal { public: void climb(); Pig(std::string theName); }; class Turtle:public Animal { public: void swim(); Turtle(std::string theName); }; void Animal::eat() { std::cout << this->name << "会吃东西 "; } void Animal::sleep() { std::cout << this->name << "会睡觉 "; } void Animal::drool() { std::cout << this->name << "会流口水 "; } void Pig::climb() { std::cout << this->name <<"会爬树 "; } void Turtle::swim() { std::cout << this->name << "会游泳 "; } /** * 构造器,子类的构造器的值是通过改变父类的值的来的 */ Animal::Animal(std::string theName) { this->name = theName; } Pig::Pig(std::string theName):Animal(theName) { } Turtle::Turtle(std::string theName):Animal(theName) { } int main(int argc, const char * argv[]) { Animal animal("大动物"); animal.eat(); animal.sleep(); animal.drool(); Pig bigPig("小母猪"); Turtle greenTurtle("小甲鱼"); bigPig.climb(); greenTurtle.swim(); return 0; }
控制台返回的结果:
大动物会吃东西
大动物会睡觉
大动物会流口水
小母猪会爬树
小甲鱼会游泳
2.继承情况下构造器和析构器的生命周期
class BaseClass { public: BaseClass(); ~BaseClass(); void doSomething(); }; class SubClass:public BaseClass { public: SubClass(); ~SubClass(); }; void BaseClass::doSomething() { std::cout << "父类做了某件事" << std::endl; } BaseClass::BaseClass() { std::cout << "父类构造器 "; } SubClass::SubClass():BaseClass() { std::cout << "子类构造器 "; } BaseClass::~BaseClass() { std::cout << "父类析构器 "; } SubClass::~SubClass() { std::cout << "子类析构器 "; } int main(int argc, const char * argv[]) { SubClass subclass; subclass.doSomething(); std::cout << "完工" << std::endl; return 0; }
控制台打印结果:
父类构造器
子类构造器
父类做了某件事
完工
子类析构器
父类析构器
3.class SubClass:private BaseClass//":private"说明只有SubClass该类可以使用基类的元素。
4.覆盖
class Animal { public: std::string mouth; void eat(); void eat(int eatCount); void sleep(); void drool(); Animal(std::string theName); protected: std::string name; }; class Pig:public Animal { public: void climb(); //overRide void eat(); Pig(std::string theName); }; class Turtle:public Animal { public: void swim(); //overRide void eat(); Turtle(std::string theName); }; void Animal::eat() { std::cout << this->name << "会吃东西 "; } void Animal::eat(int eatCount) { /** * OverLoad重载 */ std::cout << "饿了,该吃上" << eatCount << "碗饭" << std::endl; } void Animal::sleep() { std::cout << this->name << "会睡觉 "; } void Animal::drool() { std::cout << this->name << "会流口水 "; } void Pig::climb() { std::cout << this->name <<"会爬树 "; } void Pig::eat() { //overRide覆盖 Animal::eat(); std::cout << this->name << "在吃菜 "; } void Turtle::swim() { std::cout << this->name << "会游泳 "; } void Turtle::eat() { //overRide覆盖 Animal::eat(); std::cout << this->name<< "在吃海草 "; } /** * 构造器,子类的构造器的值是通过改变父类的值的来的 */ Animal::Animal(std::string theName) { this->name = theName; } Pig::Pig(std::string theName):Animal(theName) { } Turtle::Turtle(std::string theName):Animal(theName) { } int main(int argc, const char * argv[]) { Animal animal("大动物"); animal.eat(); animal.sleep(); animal.drool(); std::cout<< std::endl; std::cout<< std::endl; Pig bigPig("小母猪"); bigPig.climb(); Turtle greenTurtle("小甲鱼"); greenTurtle.swim(); std::cout<< std::endl; std::cout<< std::endl; //两个子类覆盖过的吃东西方法 bigPig.eat(); greenTurtle.eat(); std::cout << std::endl; std::cout << std::endl; //重载吃东西的方法 bigPig.Animal::eat(3); return 0; }
控制台输出结果:
大动物会吃东西
大动物会睡觉
大动物会流口水
小母猪会爬树
小甲鱼会游泳
小母猪会吃东西
小母猪在吃菜
小甲鱼会吃东西
小甲鱼在吃海草
饿了,该吃上3碗饭
5.继承下来的子类不能对继承下来的方法进行重载overLoad