C++知识点案例 笔记-3
1.基类指针等与派生类的兼容
2.构造函数
3.析构函数
4.虚基类
5.虚函数
6.虚析构函数
==基类指针等与派生类的兼容==
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
void set_age(int age){m_Age = age;}
void set_weight(int weight){m_Weight = weight;}
int get_age(){return m_Age;}
int get_weight(){return m_Weight;}
void speak(){cout<<"animal lanuage!"<<endl;}
private:
int m_Weight;
int m_Age;
};
class Dog:public Animal
{
public:
void set_name(string con_name){m_Name = con_name;}
void speak(){cout<<"wangwang!"<<endl;}
private:
string m_Name;
};
int main()
{
Dog dog;
Animal *panimal = &dog; //定义基类指针,并初始化为派生类dog的地址
panimal->set_age(6); //##################
cout<<"The dog,age = "<<panimal->get_age()<<endl; //################
dog.speak(); //dog里的函数speak
panimal->speak(); //注意虽然基类指针指向的是dog,但调用的是基类Animal里的函数speak
return 0;
}
==构造函数==
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
Animal(int con_age,int con_weight);//###############
private:
int m_Age;
int m_Weight;
};
Animal::Animal(int con_age,int con_weight)//##############
{
m_Age = con_age;
m_Weight = con_weight;
cout<<"Animal constuctor with param !"<<endl;
}
class Dog:public Animal
{
public:
Dog(string con_name,int con_weight,int con_age);//####################
private:
string m_Name;
};
Dog::Dog(string con_name,int con_weight,int con_age):Animal(con_weight,con_age)//##############
{
m_Name = con_name;
cout<<"Dog constuctor with param !"<<endl;
}
int main()
{
Dog dog("dogoo",6,8);
return 0;
}
==析构函数==
//注意:析构函数执行顺序与正常相反,即先执行派生类里的在执行基类里的
//作用:释放数据成员资源
#include <iostream>
#include <stdlib.h>
using namespace std;
class Animal
{
public:
Animal(int con_weight,int con_age);
~Animal() //#################
{
cout<<"animal constuctor diluting !!!"<<endl;
system("pause");
}
int get_age(){return m_Age;}
private:
int m_Age,m_Weight;
};
Animal::Animal(int con_weight,int con_age)
{
m_Weight = con_weight;
m_Age = con_age;
cout<<"Animal constuctor with param !"<<endl;
}
class Dog:public Animal
{
public:
Dog(string con_name,int con_weight,int con_age);
~Dog(){cout<<"dog constuctor diluting!!!"<<endl;system("pause");} //##############
private:
string m_Name;
};
Dog::Dog(string con_name,int con_weight,int con_age):Animal(con_weight,con_age)
{
m_Name = con_name;
cout<<"Dog construtor with param !"<<endl;
}
int main()
{
Dog dog("dogoo",6,8);
cout<<"Dog age ="<<dog.get_age()<<endl;
return 0;
}
==虚基类==
//避免多重继承时,派生类访问公共基类的二义性[如本例中对m_Age的访问]
#include <iostream>
using namespace std;
class Animal
{
public:
Animal(int age):m_Age(age){cout<<"Animal constructor !"<<endl;}
protected:
int m_Age; //###############
};
class Wolf:virtual public Animal //#################
{
public:
Wolf(int age,int speed):Animal(age)
{
cout<<"Wolf constructor !"<<endl;
m_Speed = speed;
}
int get_wspeed(){return m_Speed;}
private:
int m_Speed;
};
class Dog:virtual public Animal //###############
{
public:
Dog(int age,int smell):Animal(age)
{
cout<<"Dog constructor !"<<endl;
m_Smell = smell;
}
get_dsmell(){return m_Smell;}
private:
int m_Smell;
};
class WolfDog:public Wolf,public Dog
{
public:
WolfDog(int w_age,int d_age,int smell,int speed):Wolf(w_age,speed),Dog(d_age,smell),Animal(w_age) //#################
{
cout<<"WolfDog constructor !"<<endl;
}
void print_age(){cout<<"Animal age : "<<m_Age<<endl;} //####################
};
int main()
{
WolfDog tiny_wolfdog(7,6,100,120);
cout<<"The wolfdog speed :"<<tiny_wolfdog.get_wspeed()<<endl;
cout<<"The wolfdog smell :"<<tiny_wolfdog.get_dsmell()<<endl;
tiny_wolfdog.print_age(); //##################
return 0;
}
==虚函数==
//基类函数被定义为虚函数后,其派生类可重新定义该函数从而形成自己的独特风味
#include <iostream>
using namespace std;
class Animal
{
public:
virtual void speak(){cout<<"animal lanuage !"<<endl;} //##################
};
class Cat:public Animal
{
public:
virtual void speak(){cout<<"Cat will speak : miaomiao !"<<endl;} //##################
};
int main()
{
Cat cat;
cat.speak();
Animal *panimal = &cat;
panimal->speak(); //##################
return 0;
}
==虚析构函数==
//基类定义虚析构函数后,派生类的析构函数才能被调用,才可以释放派生类中申请的资源
#include <iostream>
#include <cstring>
using namespace std;
class Animal
{
public:
Animal(char *name);
void print_name();
virtual void print_color();
virtual void speak();
virtual ~Animal(); //##################
private:
char *m_Name;
};
Animal::Animal(char *name)
{
int len = strlen(name) + 1;
m_Name = new char[len];
strcpy(m_Name,name);
}
void Animal::print_name()
{
cout<<"Animal name:"<<m_Name<<endl;
}
void Animal::print_color()
{
}
Animal::~Animal() //##################
{
cout<<"Animal diluting !"<<endl;
if(m_Name){delete[]m_Name;}
}
void Animal::speak()
{
cout<<"Animal lanuage !"<<endl;
}
class Dog:public Animal
{
public:
Dog(char *name,char *dogcolor);
void print_name();
virtual void print_color();
virtual void speak();
virtual ~Dog(); //##################
private:
char *m_Color;
};
Dog::Dog(char *name,char *dogcolor):Animal(name)
{
cout<<"Dog constructor !"<<endl;
m_Color = new char[strlen(dogcolor)+1];
strcpy(m_Color,dogcolor);
}
void Dog::print_color()
{
cout<<"Dog color:" <<m_Color<<endl;
}
void Dog::speak()
{
cout<<"Dog lanuage :WangWang !"<<endl;
}
Dog::~Dog() //##################
{
cout<<"Dog diluting !"<<endl;
if(m_Color){delete[] m_Color;}
}
int main()
{
Animal *p[2];
p[0] = new Dog("dogoo","black");
p[0]->print_name();
p[0]->print_color();
p[0]->speak();
p[1] = new Dog("dogxx","white");
p[1]->print_name();
p[1]->print_color();
p[1]->speak();
for(int i = 1;i<2;i++)
{
delete p[i];
}
return 0;
}
________________________________________________________
Every good deed you do will someday come back to you.
Love you,love word !