C++ 继承与多重继承 1-5题

question 1

#include <iostream>
using namespace std;
class Point
{protected:
	float x,y;
 public:
 	Point(float xx,float yy):x(xx),y(yy) {} 
 	area() {return 0;}
 	disp() {cout<<"点("<<x<<","<<y<<")的面积为:0"<<endl;} 
 };
class Circle1:public Point
{private:
	float radius;
 public:
 	Circle1(float xx,float yy,float r):radius(r),Point(xx,yy) {} 
 	disp() {
	 Point::disp();
	 cout<<"圆心为("<<x<<","<<y<<"),半径为"<<radius<<"的圆的面积为:"<<radius*radius*3.1415926<<endl;} 
 };
class Circle2
{private:
	Point p;
	float x,y,radius;
 public:
 	Circle2(float xx,float yy,float r):x(xx),y(yy),radius(r),p(xx,yy) {} 
 	disp() { p.disp();
	         cout<<"圆心为("<<x<<","<<y<<"),半径为"<<radius<<"的圆的面积为:"<<radius*radius*3.1415926<<endl;
	        } 
};
int main()
{   Circle1 c1(9,2,10);
    Circle2 c2(0,2,10);
    Point p(1,2);
    
    p.disp();
    cout<<endl;
	c1.disp();//派生类继承point 
	cout<<endl;
    c2.disp();//类中有子对象 
  return 0;
}

question 2

#include<iostream>
using namespace std;
class Time;
class Date
{protected:
		int year,month,day;
 public:
		Date()//无参构造函数 
		{   cout<<"year=";cin>>year;
		    cout<<"month=";cin>>month;
		    cout<<"day=";  cin>>day;
		}
		Date(int y,int m,int d):year(y),month(m),day(d) {} 
		void printdata()
		{   cout<<year<<"年"<<month<<"月"<<day<<"日 ";
		}
}; 
class Time
{protected:
	int hour,minute,second;
public:
	Time() //无参构造函数 
	{   cout<<"hour=";cin>>hour;
	    cout<<"minute=";cin>>minute;
	    cout<<"second=";cin>>second;
	}
	Time(int h,int m,int s):hour(h),minute(m),second(s) {} 
	void printtime()
	{   cout<<hour<<"时"<<minute<<"分"<<second<<"秒"<<endl; 
	}
};
class TimeDate:public Date,public Time
{
 public:
    TimeDate(int y,int m1,int d,int h,int m2,int s):Date(y,m1,d),Time(h,m2,s) {}
    TimeDate() {} //基类构造函数隐含在派生类的默认构造函数,无需显示写出,且构造顺序按照继承顺序 
	void print()  //该派生类继承顺序是Date-Time,默认构造函数即无参构造函数函数 
	{   Date::printdata();
	    Time::printtime();
	}
};
int main()
{ 
  //Date d;
  //Time t;
  //d.printdata();
  //t.printtime();
  TimeDate td0,td(2021,5,17,16,42,12);//无参构造对象与有参构造对象 
  td0.print();
  td.print();
  return 0;
}
 

question 3

#include<iostream>
using namespace std; 
class Book
{
public:
	Book(int n,string na,int p,string pr,int y,int m):no(n),name(na),price(p),press(pr),year(y),month(m){}
	void disp_no(){cout<<"书号:"<<no<<endl;}
	void disp_name(){cout<<"书名:"<<name<<endl;}
	void disp_price(){cout<<"定价:"<<price<<endl;}
	void disp_ym(){cout<<"出版时间:"<<year<<'.'<<month<<endl;}
protected:
	int no;          //书号
	string name;     //书名
	double price;    //定价
	string press;    //出版社
	int year,month;  //出版时间
};

class Author
{
public:
	Author(string na,int a,int y):name(na),age(a),year(y){}
	void disp_name(){cout<<"作者姓名:"<<name<<endl;}
	void disp_age(){cout<<"作者年龄:"<<age<<endl;}
	void disp_year(){cout<<"写作时间:"<<year<<endl;}
protected:
	string name;  //作者姓名
	int age;      //作者年龄
	int year;     //写作时间
};
class Card:public Book,public Author
{public:
	Card(int n,string na1,int p,string pr,int y1,int m,string na2,int a,int y2,int nob)
	:Book(n,na1,p,pr,y1,m),Author(na2,a,y2),no(nob){}
	void disp()
	{   cout<<"卡号:"<<no<<endl;
	    disp_no();
		Book::disp_name();//disp_name两个基类都有,而且重名,所以。
		disp_price();
		disp_ym();
		Author::disp_name();
		disp_age();
		disp_year();
	}
 private:
 	int no;
};
int main()
{   Card ba1(2017,"被讨厌的勇气",30.69,"商务出版社",2002,2,"阿勒德日本铁杆粉",56,2002,30256);
    ba1.disp();
	return 0;
}

question 4   长方体类的面积我就写个寂寞,默认边与坐标轴平行就可以确定矩形

由面积公式可知,直径端点固定的情况下,三角形的面积是不固定的,所以对称得来的矩形面积也随thelta的变化而变化

#include <iostream>
#include <math.h>
#include <string>
#define N 20
using namespace std;
class  Shape
{public:
       Shape() {};  
	   virtual void disp() {}
};
class Circle:public Shape
{protected:
	float radius,x,y;
 public:
 	//Circle(float xx,float yy,float r):x(xx),y(yy),radius(r) {} 
 	Circle() {}
	void set() 
	{   cout<<"请输入圆心坐标和半径:";cin>>x>>y>>radius; 
	}
 	void disp() {cout<<"圆心为("<<x<<","<<y<<"),半径为"<<radius<<"的圆的面积为:"<<radius*radius*3.1415926<<endl;} 
 };
class Rectangle:public Shape
{protected:
	float x1,x2;
	float y1,y2;
 public:
 	//Rectangle(float xx1,float yy1,float xx2,float yy2,float xx3,float yy3)
	// :x1(xx1),y1(yy1),x2(xx2),y2(yy2),x3(xx3),y3(yy3) {}
	Rectangle( ) {} 
	void set()
	{   cout<<"请输入A,B两点的点坐标(二维):"<<endl;
	    cin>>x1>>y1;
		cin>>x2>>y2;
		while(x1==x2&&y1==y2)
		{ cout<<"A,B两点坐标重合,请重新输入";
		  cin>>x2>>y2;
		}
	}
	void disp() {cout<<"the area of the Rectangle is "<<abs(x1-x2)*abs(y1-y2)<<endl;} 
};
class Triangle:public Shape
{protected:
	float x1,x2,x3;
	float y1,y2,y3;
	float p,a,b,c;
 public:
 	//Triangle(float xx1,float yy1,float xx2,float yy2,float xx3,float yy3)
 	//:x1(xx1),y1(yy1),x2(xx2),y2(yy2),x3(xx3),y3(yy3) {}
 	Triangle() {} 
 	void set()
	{   cout<<"请输入A,B,C三点的点坐标(2d):"<<endl;
	    cout<<"A点:";
	    cin>>x1>>y1; 
	    cout<<"B点:";
	    cin>>x2>>y2;
	    while(x1==x2&&y1==y2)
	    {cout<<"A,B两点坐标重合,请重新输入";
		  cin>>x2>>y2;
		}
		cout<<"C点:"; 
		cin>>x3>>y3;
		while( (x1==x3&&y1==y3) || (x2==x3&&y2==y3) )
		{cout<<"C点与A或B重合,请重新输入:";
		 cin>>x3>>y3;
		 } 
	}
   
	void disp() 
	{   a = sqrt(pow(x1-x2,2)+pow(y1-y2,2)) ;
	    b = sqrt(pow(x1-x3,2)+pow(y1-y3,2)) ;
	    c = sqrt(pow(x2-x3,2)+pow(y2-y3,2)) ;
	    p = (a+b+c)/2 ; 
	    cout<<"the area of the Triangle is "<<sqrt(p*(p-a)*(p-b)*(p-c))<<endl;} 
};
int main()
{   int num=0;
    char ch[N],GET;
    Shape  *shape[N];
    Circle *c;
    Rectangle *r;
    Triangle *t;
    cout<<"最多创建"<<N<<"个对象"<<endl;
	do
	{ 
	  cout<<"C 圆对象    R  长方形对象   T  三角形对象    X 表示结束"<<endl;
	  ch[num] = getchar();
	  if( ch[num]  == 'X'){
		 break;
	 }
	  if( ch[num] == 'C'){
	     c = new Circle; 
	     shape[num] = c;
	     num++;
	     c->set();
	                }
	  if( ch[num] == 'R'){
	  	 r = new Rectangle;
	  	 shape[num] = r;
	  	 num++;
	  	 r->set();
	  } 
	  if( ch[num] == 'T'){
	  	t = new Triangle;
	  	shape[num] = t;
	  	num++;
	  	t->set();
	  }
	  cout<<"对象创建成功"<<endl; 
      GET = getchar();//字符变量接收小习惯,防止回车被下一次ch接收 
	} while(ch[num-1] != 'X');
	
	cout<<"循环输出 "<<num<<"个对象"<<endl;
	
	for(int i = 0;i<num;i++)
	{   if( ch[i]  == 'X')
	     continue;
	  if( ch[i] == 'C'){ 
	     shape[i]->disp();
	                }
	  if( ch[i] == 'R'){
	  	 shape[i]->disp();
	  } 
	  if( ch[i] == 'T'){
	  	shape[i]->disp();
	  }
	  }
	 delete []*shape;
	 delete []c;
	 delete []r;
	 delete []t; 
	return 0;
}
    

question 5

相对于question4的代码,唯一的变动就是基类的disp函数。。。

#include <iostream>
#include <math.h>
#include <string>
#define N 20
using namespace std;
class  Shape
{public:
       Shape() {};  
	   virtual void disp() = 0 ;
};
class Circle:public Shape
{protected:
	float radius,x,y;
 public:
 	//Circle(float xx,float yy,float r):x(xx),y(yy),radius(r) {} 
 	Circle() {}
	void set() 
	{   cout<<"请输入圆心坐标和半径:";cin>>x>>y>>radius; 
	}
 	void disp() {cout<<"圆心为("<<x<<","<<y<<"),半径为"<<radius<<"的圆的面积为:"<<radius*radius*3.1415926<<endl;} 
 };
class Rectangle:public Shape
{protected:
	float x1,x2;
	float y1,y2;
 public:
 	//Rectangle(float xx1,float yy1,float xx2,float yy2,float xx3,float yy3)
	// :x1(xx1),y1(yy1),x2(xx2),y2(yy2),x3(xx3),y3(yy3) {}
	Rectangle( ) {} 
	void set()
	{   cout<<"请输入A,B两点的点坐标(二维):"<<endl;
	    cin>>x1>>y1;
		cin>>x2>>y2;
		while(x1==x2&&y1==y2)
		{ cout<<"A,B两点坐标重合,请重新输入";
		  cin>>x2>>y2;
		}
	}
	void disp() {cout<<"the area of the Rectangle is "<<abs(x1-x2)*abs(y1-y2)<<endl;} 
};
class Triangle:public Shape
{protected:
	float x1,x2,x3;
	float y1,y2,y3;
	float p,a,b,c;
 public:
 	//Triangle(float xx1,float yy1,float xx2,float yy2,float xx3,float yy3)
 	//:x1(xx1),y1(yy1),x2(xx2),y2(yy2),x3(xx3),y3(yy3) {}
 	Triangle() {} 
 	void set()
	{   cout<<"请输入A,B,C三点的点坐标(2d):"<<endl;
	    cin>>x1>>y1; 
	    cin>>x2>>y2;
	    while(x1==x2&&y1==y2)
	    {cout<<"A,B两点坐标重合,请重新输入";
		  cin>>x2>>y2;
		}
		cin>>x3>>y3;
		while( (x1==x3&&y1==y3) || (x2==x3&&y2==y3) )
		{cout<<"C点与A或B重合,请重新输入:";
		 cin>>x3>>y3;
		 } 
	}
   
	void disp() 
	{   a = sqrt(pow(x1-x2,2)+pow(y1-y2,2)) ;
	    b = sqrt(pow(x1-x3,2)+pow(y1-y3,2)) ;
	    c = sqrt(pow(x2-x3,2)+pow(y2-y3,2)) ;
	    p = (a+b+c)/2 ; 
	    cout<<"the area of the Triangle is "<<sqrt(p*(p-a)*(p-b)*(p-c))<<endl;} 
};
int main()
{   int num=0;
    char ch[N],GET;
    Shape  *shape[N];
    Circle *c;
    Rectangle *r;
    Triangle *t;
    cout<<"最多创建"<<N<<"个对象"<<endl;
	do
	{ 
	  cout<<"C 圆对象    R  长方形对象   T  三角形对象    X 表示结束"<<endl;
	  ch[num] = getchar();
	  if( ch[num]  == 'X'){
		 break;
	 }
	  if( ch[num] == 'C'){
	     c = new Circle; 
	     shape[num] = c;
	     num++;
	     c->set();
	                }
	  if( ch[num] == 'R'){
	  	 r = new Rectangle;
	  	 shape[num] = r;
	  	 num++;
	  	 r->set();
	  } 
	  if( ch[num] == 'T'){
	  	t = new Triangle;
	  	shape[num] = t;
	  	num++;
	  	t->set();
	  }
	  cout<<"对象创建成功"<<endl; 
      GET = getchar();//字符变量接收小习惯,防止回车被下一次ch接收 
	} while(ch[num-1] != 'X');
	
	cout<<"循环输出 "<<num<<"个对象"<<endl;
	
	for(int i = 0;i<num;i++)
	{   if( ch[i]  == 'X')
	     continue;
	  if( ch[i] == 'C'){ 
	     shape[i]->disp();
	                }
	  if( ch[i] == 'R'){
	  	 shape[i]->disp();
	  } 
	  if( ch[i] == 'T'){
	  	shape[i]->disp();
	  }
	  }
	 delete []*shape;
	 delete []c;
	 delete []r;
	 delete []t; 
	return 0;
}
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值