C++郑莉课后题-第七章-继承与派生-编程部分

构造函数先基后派,先基后派,先基后派

7-5 定义一个基类Shape,在此基础上派生出Rectangle和Cirele,二者都有getArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。


——派生里重写的函数在基类里要virtual;

——基类指针可以指派生类,父可以指子,父可以指子,父可以指子

#include<iostream>
#include<string.h>
using namespace std;
class Shape{
public:	
virtual double getArea(){ 	}
};

class Rectangle:public Shape{
	double w,h;
	public:
		Rectangle(double ww=0,double hh=0):w(ww),h(hh){
		}
		virtual double getArea(){
			return w*h;
		}
		virtual double getw(){
			return w;
		}
		virtual double geth(){
			return h;
		}
};

class Square:public Rectangle{
	public:
		Square(double x=0):Rectangle(x,x){		}
		double getArea(){ return Rectangle::getArea();
		}
};
class Circle:public Shape{
	double r;
	public:
		Circle(double rr=0):r(rr){		}
		double getArea(){
		return r*r*3.1415926;
		}
};

int main(){
  Shape *p;
  p=new Circle(5); //基类可以指向派生类
  cout<<"圆面积"<<p->getArea()<<endl;
  delete p; 
  p=new Rectangle(2,3);
  cout<<"长方形面积"<<p->getArea()<<endl;
  delete p; 
  p=new Square(2);
  cout<<"正方形面积"<<p->getArea()<<endl;
  delete p; 
  
	return 0;
}

7-6 定义一个哺乳动物类Mammal,再由此派生出狗类Dog,定义一个Dog类的对象,观察基类与派生类的构造函数和析构函数的调用顾序。

#include<iostream>
#include<string.h>
using namespace std;

class Mammal{
	int age,weight;
	public:
		Mammal(int a=0,int w=0):age(a),weight(w){cout<<"Mammal析构"<<endl;			}
		~Mammal(){ cout<<"Mammal析构"<<endl;		}
		int getage()const{ return age;  }
		void setage(int a){age=a;  }
		int getweight()const{return weight;  }
		void setweight(int w){ w=weight;  }
		virtual void speak()const{
		cout<<"Mammal speak"<<endl;
		}
};
enum color{
	white,yello,black
};
class Dog:public Mammal{
	color dogcolor;
	public:
		Dog(int a=0,int w=0,color co=white):Mammal(a,w){ cout<<"狗构造"<<endl;}
		~Dog(){ cout<<"狗析构"<<endl;		}	
		color getcolor()const{ return dogcolor; }
		void setcolor(color c){ dogcolor=c; }
};
int main(){
  Dog d(3,5,white);
  d.speak(); 
	return 0;
}

构造先基后派

7-8 定义一个Document类,有数据成员name,从Document派生出Book类,增加数据成员pagcCount.

#include<iostream>
#include<string.h>
using namespace std;

class Document{
	string name;
	public:
		Document(){		}
		Document(string n):name(n){		}
		void changename(string n){	name=n;}
		string getname()const{ cout<<name<<endl; return name;		}
};

class Book:public Document{
	long pagcCount;
	public:
		Book(){		}
		Book(string n,long c):Document(n),pagcCount(c){}
		~Book(){}
		void printBookname()const{Document::getname(); }
};
int main(){
  Document d("Ado");
  Book b("book",10);
  b.printBookname();
	return 0;
}

7-9定义基类Base,有两个共有成员函数fn10、fn20, 私有派生出Derived类,如果想在Derived类的对象中使用基类函数fn10,应怎么办?
 

#include<iostream>
#include<string.h>
using namespace std;

class Base{
	public:
		void fn10(){ cout<<"fn10"<<endl;}
		void fn20(){ cout<<"fn20"<<endl;}
};

class Derived:public Base{
public:
	int fn10(){ cout<<"D:"<<endl;Base:: fn10();}
	int fn20(){ cout<<"D:"<<endl;Base:: fn20();}
};

int main(){
  Derived d;
  d.fn10();
  d.fn20();
	return 0;
}

7-10定义object 类,有weight属性及相应的操作函数,由此派生出box类,增加Height和width属性及相应的操作函数,声明一个box对象,观察构造函数与析构函数的调用顺序。
 

#include<iostream>
#include<string.h>
using namespace std;

class object{
	int weight;
	public:
		object(int w=0):weight(w){	cout<<"create object"<<endl;	}
		~object(){cout<<"delete object"<<endl;}
		int getweight()const{ return weight;}
		void setweight(int w){  weight=w;}
};
class box:public object{
	int Height,width;
	public:
		box(int h=0,int wi=0,int we=0):Height(h),width(wi),object(we){	cout<<"create box "<<endl;	}
		~box(){cout<<"delete box"<<endl;}
		int getHeight()const{ return Height;}
		void setHeight(int h){ Height=h;}
		int getwidth()const{ return width;}
		void setwidth(int wi){  width=wi;}
};

int main(){
  box b;
  
	return 0;
}

!! 7-11定义一个基类BaseClass,从它派生出类DerivedClass, BaseClass 有成员函数fn10、fn2(, DerivedClass 也有成员函数fnl0、fn2(), 在主程序中定义一个DerivedClass的对象,分别用DerivedClass 的对象以及BaseClass和DerivedClass的指针来调用fn10、fn20, 观察运行结果。

#include<iostream>
#include<string.h>
using namespace std;

class BaseClass{
	public:
	void fn1(){cout<<"BCf1"<<endl;}
	void fn2(){cout<<"BCf2"<<endl;}
	
};
class DerivedClass:public BaseClass{
	public:
	void fn1(){cout<<"DCf1"<<endl;}
	void fn2(){cout<<"DCf2"<<endl;}
	
};

int main(){
DerivedClass d;
DerivedClass  *dp= &d  ;
BaseClass *db=&d ;
d.fn1(); //DCf1
d.fn2();//DCf2
dp->fn1();// DCf1  //指针用->不用· 
dp->fn2();// DCf2
db->fn1();//BCf1
db->fn2();//BCf1
	return 0;
}

 基类指针->基类的方法。哪怕*p=&派生类对象

基类指针->基类的方法。哪怕*p=&派生类对象

基类指针->基类的方法。哪怕*p=&派生类对象

7-12 组合与继承有什么共同点和差异?通过组合生成的类与被组合的类之间的逻辑关系是什么?继承呢?

组合:A有一个B;我有一把笔

继承:A是一个B;猫是动物

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值