处理菱形继承问题&&实现一个虚函数的覆盖及调用&&实现以下几个类的成员函数

#include <iostream>

#include <string>

using namespace std;

1.实现以下几个类的成员函数

2.实现一个虚函数的覆盖及调用

3.处理菱形继承问题。

植物

class Botany

{

public:

//(const string& name) // const char* name

Botany(const char* name = "")

:_name(name) //构造函数

{

//cout<<"Botany(const char* name)"<<endl;

}

Botany(const Botany& b)

:_name(b._name)

{}

Botany& operator= (const Botany& b)

{

if (this != &b)

{

_name = b._name;

}

return *this;

}

~Botany()

{}

virtual void Display ()

{

cout<<"Botany::Display"<<endl;

}

public:

protected:

string _name; //名字

};

class Tree : virtual public Botany

{

public:

Tree(const string& name, int hight)

:Botany(name.c_str())

,_hight(hight)

{}

Tree(const Tree& t)

:Botany(t)

,_hight(t._hight)

{}

Tree& operator=(const Tree& t)

{

if (this != &t)

{

Botany::operator=(t);

_hight = t._hight;

}

return *this;

}

~Tree()

{}

virtual void Display ()

{

cout<<"Tree::Display"<<endl;

}

public:

int _hight; // 高度

};

class Flower : virtual public Botany

{

public:

Flower(const string& name, int colour)

:Botany(name.c_str())

,_colour(colour)

{}

Flower(const Flower& t)

:Botany(t)

,_colour(t._colour)

{}

Flower& operator=(const Flower& t)

{

if (this != &t)

{

Botany::operator=(t);

_colour = t._colour;

}

return *this;

}

~Flower()

{}

public:

protected:

int _colour; // 颜色

};

白兰花,即是树有时花。

class MicheliaAlba : public Tree, public Flower

{

public:

// 必须先初始化虚基类的构造函数

MicheliaAlba(const string& name, int hight, int colour)

:Botany(name.c_str())

,Flower(name, colour)

,Tree(name, hight)

{}

MicheliaAlba(const MicheliaAlba& m)

:Botany(m._name.c_str())

,Flower(m)

,Tree(m)

{}

MicheliaAlba& operator=(const MicheliaAlba& m)

{

if (this != &m)

{

Flower::operator=(m);

Tree::operator=(m);

}

return *this;

}

~MicheliaAlba()

{}

void Display()

{

//cout<<_name<<endl;

cout<<Flower::_name<<endl;

cout<<Tree::_name<<endl;

}

protected:

};

void Test()

{

Botany b1("xxx");

Tree t1("xxx", 1);

Botany& r1 = b1;

r1.Display();

Botany& r2 = t1;

r2.Display();

Botany* p3 = &t1;

p3->Display();

}

int main()

{

//Test();

MicheliaAlba m1("白玉兰", 10, 1);

MicheliaAlba m2("黄玉兰", 15, 2);

cout<<&(m1.Flower::_name)<<endl;

cout<<&(m1.Tree::_name)<<endl;

m1.Display();

return 0;

}

处理菱形继承问题&&实现一个虚函数的覆盖及调用&&实现以下几个类的成员函数

时间: 2024-08-08 01:16:03

处理菱形继承问题&&实现一个虚函数的覆盖及调用&&实现以下几个类的成员函数的相关文章

在类的成员函数中调用delete this

在类的成员函数中能不能调用delete this?答案是肯定的,能调用,而且很多老一点的库都有这种代码.假设这个成员函数名字叫release,而delete this就在这个release方法中被调用,那么这个对象在调用release方法后,还能进行其他操作,如调用该对象的其他方法么?答案仍然是肯定 的,调用release之后还能调用其他的方法,但是有个前提:被调用的方法不涉及这个对象的数据成员和虚函数.说到这里,相信大家都能明白为什么会这样 了. 根本原因在于delete操作符的功能和类对象的

作为类的成员函数,重载运算符只能有一个参数

1 overload a operator of a class, you can only use one para., this pointer is automatically used. class Rational { public: //not correct since this ponit would be used automatically. //Rational operator+ (const Rational& lhs, const Rational& rhs);

类的成员函数指针(比較深入)

From:http://blog.csdn.net/hairetz/archive/2009/05/06/4153252.aspx 个人感觉对于类的成员函数指针这块解说的比較深入具体 推荐阅读 ///////////////////////////////////////////////// 先看这样一段代码 class test {    public:       test(int i){ m_i=i;}       test(){} void hello()       {        

(转)c++类的成员函数存储方式(是否属于类的对象)---一道面试题引发的思考

昨天去面试一家公司,面试题中有一个题,自己没弄清楚,先记录如下: class D { public: void printA() { cout<<"printA"<<endl; } virtual void printB() { cout<<"printB"<<endl; } }; main函数调用: D *d=NULL; d->printA(); d->printB(); 输出结果是? 当时想的是对象d直

Delphi函数详解:全局函数,内部函数,类的成员函数,类的静态方法

1. Delphi中的全局函数 //要点: 需要给其他单元调用, 必须在 interface 声明, 但必须在 uses 区后面 unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrls; type   TForm1 = class(TForm)     Button1: TButton;     proce

linux 之 pthread_create 实现类的成员函数做参数

在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create创建线程,线程函数是一个全局函数,所以在C++中,创建线程时,也应该使用一个全局函数.static定义的类的成员函数就是一个全局函数. class Thread { private: pthread_t pid; private: static void * start_thread(void *a

类的成员函数实现线程的回调函数

一般都是用静态函数作为线程的回调函数实现,但是总是感觉不是很顺畅,更改吧,就好像破坏了类的封装性,不改吧,访问实在是麻烦.所以,今天要做的就是让类的成员函数作为线程的回调函数存在,其中使用的一个比较特殊的结构就是 union { void ( *ThreadProc)(LPVOID pvParam); void ( student::*MemberProc)(LPVOID pvParam); } Proc; 联合类,用于转换类成员方法指针到普通函数指针 下面是一个小李子,变量名 就凑活看吧,核心

C++类的成员函数使用的一些小总结

From: http://blog.csdn.net/xiayefanxing/article/details/7607506 这一阵做项目代码开发的时候,用到了在一个C++文件中使用另一个类的成员函数的问题,做个小总结. 其中有些是网上搜索的资料,因为比较分散就不一一给出出处了,请作者见谅. 1.C++如何在一个类的成员函数中调用另一个类的成员函数? 假设你想在类A里调用类B的函数int f(x),两种办法: (1)class A::B 也就是说将B定义为A的父类, 这样你就可以自然的在A里面

const修饰类的成员函数

<Effective C++>里面说,尽量使用const,const修饰变量一般有两种方式:const T *a,或者 T const *a,这两者都是一样的,主要看const位于*的左边还是右边,这里不再赘述,主要来看一下当const修饰类的成员函数时,成员函数有什么特点. 类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作任何改变. 在设计类的时候,一个原则就是对于不改变数据成员的成员函数都要在后面加 const,而对于改变数据成员的成员函