-
父子关系 成员同名 隐藏
查看全部 -
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
/**
* 定义人类: Person
*/
class Person
{
public:
Person()
{
cout << "Person" << endl;
}
~Person()
{
cout << "~Person" << endl;
}
void eat()
{
cout << "eat" << endl;
}
};
/**
* 定义工人类: Worker
* 虚继承人类
*/
class Worker : virtual public Person
{
public:
Worker(string name)
{
m_strName = name;
cout << "Worker" << endl;
}
~Worker()
{
cout << "~Worker" << endl;
}
void work()
{
cout << m_strName << endl;
cout << "work" << endl;
}
protected:
string m_strName;
};
/**
* 定义儿童类:Children
* 虚继承人类
*/
class Children : virtual public Person
{
public:
Children(int age)
{
m_iAge = age;
cout << "Children" << endl;
}
~Children()
{
cout << "~Children" << endl;
}
void play()
{
cout << m_iAge << endl;
cout << "play" << endl;
}
protected:
int m_iAge;
};
/**
* 定义童工类:ChildLabourer
* 公有继承工人类和儿童类
*/
class ChildLabourer:public Worker,public Children
{
public:
ChildLabourer(string name, int age):Worker(name),Children(age)
{
cout << "ChildLabourer" << endl;
}
~ChildLabourer()
{
cout << "~ChildLabourer" << endl;
}
};
int main(void)
{
// 用new关键字实例化童工类对象
ChildLabourer *p = new ChildLabourer("Jim",13);
// 调用童工类对象各方法。
p->eat();
p->work();
p->play();
delete p;
p = NULL;
return 0;
}查看全部 -
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
/**
* 定义工人类: Worker
* 数据成员: m_strName
* 成员函数: work()
*/
class Worker
{
public:
Worker(string name)
{
m_strName = name;
cout << "Worker" << endl;
}
~Worker()
{
cout << "~Worker" << endl;
}
void work()
{
cout << m_strName << endl;
cout << "work" << endl;
}
protected:
string m_strName;
};
/**
* 定义儿童类: Children
* 数据成员: m_iAge
* 成员函数: play()
*/
class Children
{
public:
Children(int age)
{
m_iAge = age;
cout << "Children" << endl;
}
~Children()
{
cout << "~Children" << endl;
}
void play()
{
cout << m_iAge << endl;
cout << "play" << endl;
}
protected:
int m_iAge;
};
/**
* 定义童工类: ChildLabourer
* 公有继承工人类和儿童类
*/
class ChildLabourer : public Worker,public Children
{
public:
ChildLabourer(string name, int age):Worker(name),Children(age)
{
cout << "ChildLabourer" << endl;
}
~ChildLabourer()
{
cout << "~ChildLabourer" << endl;
}
};
int main(void)
{
// 使用new关键字创建童工类对象
ChildLabourer *childLabourer = new ChildLabourer("sam",12);
// 通过童工对象调用父类的work()和play()方法
childLabourer->work();
childLabourer->play();
// 释放
delete childLabourer;
childLabourer = NULL;
return 0;
}查看全部 -
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
/**
* 定义人的类: Person
* 数据成员姓名: m_strName
* 成员函数: eat()
*/
class Person
{
public:
string m_strName;
void eat()
{
cout << "eat" << endl;
}
};
/**
* 定义士兵类: Soldier
* 士兵类公有继承人类: public
* 数据成员编号: m_strCode
* 成员函数: attack()
*/
class Soldier:public Person
{
public:
string m_strCode;
void attack()
{
cout << "fire!!!" << endl;
}
};
int main(void)
{
// 创建Soldier对象
Soldier soldier;
// 给对象属性赋值
soldier.m_strName = "Jim";
soldier.m_strCode = "592";
// 打印对象属性值
cout << soldier.m_strName << endl;
cout << soldier.m_strCode << endl;
// 调用对象方法
soldier.eat();
soldier.attack();
return 0;
}查看全部 -
不同继承方式下,对于数据成员访问的情况
查看全部 -
保护继承方式
查看全部 -
公有继承方式
查看全部 -
由代码可知此时顶层父类的参数没有改变,即顶层父类无法从子类中获得传入参数
查看全部 -
虚继承(加上virtual关键字)后,Person()构造函数仅执行一次,注意顺序
查看全部 -
注意构造/析构函数调用顺序(普通继承,非虚继承)
查看全部 -
#ifndef #define …… #endif
宏定义解决重定义类的问题
查看全部 -
B public继承 A:
A public --> B public
A protected --> B protected
A private --> B 不可访问
B protected继承 A:
A public,protected --> B protected
A private --> B无法访问
B private继承A:
A public,protected -->B private
A private --> B无法访问
查看全部 -
先调用基类构造函数(按初始化列表顺序),再调用子类构造函数
查看全部 -
test1 以对象为参数,会实例化一个对象副本并在函数结束时自动销毁;
test2 以引用为参数,不会产生新的对象,只是产生一个引用,因此不会调用构造/析构函数;
test3 以指针为参数,使用时以&p为参数,结果与test2相同
查看全部 -
子类与父类间同名函数只能进行隐藏,不能进行重载!
查看全部
举报