c++自我理解的概念

来源:互联网 发布:药物查询软件 编辑:程序博客网 时间:2024/05/16 15:41

http://blog.csdn.net/hackbuteer1/article/details/7475622

http://songxing10000.blog.163.com/blog/static/163195441201482703115562/

多态:当子类指针指向父类对象时候(貌似这句说反了,应该是父类指针指向子类对象 这句才是对的 ,括号里的是对的),通过虚函数重写父类接口函数,这样使得各个子类表现出不同的形态。

father * p=new son;

私有成员只能在类内访问不能在类外访问,只能通过共有接口或有元函数在类外访问

int b;

int* a=&b;

等于谁则指向谁 a=&b  a指向b  a->b


实函数表示实实在在存在的,以后不能通过继承来改的。虚函数则表示可以改动的函数,通过继承来改从而实现多肽。

虚继承表示:只取一份父亲母亲从大boss继承的财产给儿子

               A

/ (vitrue)              \ (vitrue) 

B                          C

\                           / 

              D


 #include<iostream>
using namespace std;
class Human
{
public:
Human() { cout << "构造人类\n"; }
virtual void smart() {}
virtual void beautiful() {}
virtual ~Human() { cout << "析构人类\n"; }
};
class father:virtual public Human
{
public:
father() { cout << "构造父亲\n"; }
virtual void smart() { cout << "父亲很聪明\n"; }
virtual ~father() { cout << "析构父亲" << endl; }
};
class mother:virtual public Human
{
public:
mother() { cout << "构造母亲\n"; }
virtual void beautiful() { cout << "母亲很漂亮\n"; }
virtual ~mother() { cout << "析构母亲" << endl; }
};

class son :public father,public mother
{
public:
son() { cout << "构造儿子\n"; }
virtual void beautiful() { cout << "儿子也很帅\n"; }
virtual void smart() { cout << "儿子也很聪明\n"; }
    virtual ~son() { cout << "析构儿子" << endl; }
};
int main()
{
Human *p;
int choice = 0;
while (choice < 99)
{
cout << "(0)退出(1)父类(2)子类(3)母类";
cin >> choice;
bool quit = false;
switch (choice)
{
case 0:quit = true;
break;
case 1:p = new father;
p->smart();
delete p;
break;
case 2:p = new son;
    p->beautiful();
p->smart();
delete p;
break;
default:cout << "请输入0到2之间的数字。\n";
break;
}
if (quit)
{
break;
}
}
return 0;
}
/*
2

输出结果:
(0)退出(1)父类(2)子类2
构造人类
构造父亲
构造母亲
构造儿子
儿子也很漂亮
儿子也很帅
析构儿子
析构母亲
析构父亲
析构人类
*/


纯虚函数的功能完全靠子函数实现




 #include<iostream>
using namespace std;
class Animal
{
public:
Animal(int);
virtual ~Animal() { cout << "析构动物\n"; }
virtual int GetAge() { return itsage; }
virtual void Sleep() = 0;
virtual void Propagate() = 0;
virtual void Eat() = 0;
virtual void Move() = 0;
virtual void Body() = 0;
virtual void Show() = 0;
private:
int itsage;
};
Animal::Animal(int age) :itsage(age){ cout << "创建动物\n"; }
class Mammalia :public Animal
{
public:
Mammalia(int age) :Animal(age){ cout << "创建哺乳类\n"; }
virtual ~Mammalia() { cout << "析构哺乳类\n"; }
virtual void Propagate(){ cout << "哺乳类动物是胚胎动物\n"; }
};
class Bird :public Animal
{
public:
Bird(int age) :Animal(age) { cout << "创建鸟类\n"; }
virtual ~Bird() { cout << "析构鸟类\n"; }
virtual void Sleep() { cout << "鸟类站着睡\n"; }
virtual void Propagate() { cout << "鸟类卵生\n"; }
virtual void Eat() { cout << "鸟吃\n"; }
virtual void Move() { cout << "鸟飞\n"; }
virtual void Body() { cout << "鸟羽毛\n"; }
virtual void Show() { cout << "鸟的寿命" << GetAge() << endl; }
};
class Human :public Mammalia
{
public:
Human(int age) :Mammalia(age){ cout << "创建人类\n"; }//一般继承了谁就初始化列表里初始化谁
//Human(int age) :Animal(age){ cout << "创建人类\n"; } //2
virtual ~Human(){ cout << "析构人类\n"; }
virtual void Sleep() { cout << "人类站着睡\n"; }
virtual void Propagate() { cout << "人类胚胎\n"; }
virtual void Eat() { cout << "人吃\n"; }
virtual void Move() { cout << "人飞\n"; }
virtual void Body() { cout << "人羽毛\n"; }
virtual void Show() { cout << "人的寿命" << GetAge() << endl; }
};
class Pig :public Mammalia
{
public:
Pig(int age) :Mammalia(age){ cout << "创建猪类\n"; }
virtual ~Pig(){ cout << "析构猪类\n"; }
virtual void Sleep() { cout << "猪类站着睡\n"; }
virtual void Propagate() { cout << "猪类胚胎\n"; }
virtual void Eat() { cout << "猪吃\n"; }
virtual void Move() { cout << "猪飞\n"; }
virtual void Body() { cout << "猪羽毛\n"; }
virtual void Show() { cout << "猪的寿命" << GetAge() << endl; }
};
int main()
{
Animal *p=0;
int choice;
bool quit = false;
while (1)
{
cout << "(1)猪(2)人(3)鸟(0)退出:\n";
cin >> choice;
switch (choice)
{
case 1:p = new Pig(1);
break;
case 2:p = new Human(80);
break;
case 3:p = new Bird(50);
break;
default:quit = true;
}
if (quit)
{
break;
}
p->Body();
p->Eat();
p->Move();
p->Propagate();
p->Show();
p->Sleep();
delete p;
}
return 0;
}

/*

(1)猪(2)人(3)鸟(0)退出:
2
创建动物
创建哺乳类
创建人类
人羽毛
人吃
人飞
人类胚胎
人的寿命80
人类站着睡
析构人类
析构哺乳类
析构动物
(1)猪(2)人(3)鸟(0)退出:

*/



/*
如果执行2会出现如下错误
 error C2512: 'Mammalia' : no appropriate default constructor available
 error C2614: 'Human' : illegal member initialization: 'Animal' is not a base or member
*/



3、对于带类的c++调用函数一般是:v1.front() ;   即:对象.函数

而c语言直接调用函数: front()  ;

0 0
原创粉丝点击