pta23面向对象程序设计实验4继承与派生

7-1 定义基类Point和派生类Circle,求圆的周长.

分数 10

全屏浏览

切换布局

作者 沙金

单位 石家庄铁道大学

定义基类Point(点)和派生类Circle(圆),求圆的周长。Point类有两个私有的数据成员float x,y;Circle类新增一个私有的数据成员半径float r和一个公有的求周长的函数getCircumference();主函数已经给出,请编写Point和Circle类。

#include <iostream>
#include<iomanip>
using namespace std;
//请编写你的代码
int main()
{
    float x,y,r;
    cin>>x>>y>>r;
    Circle c(x,y,r);
    cout<<fixed<<setprecision(2)<<c.getCircumference()<<endl;
    return 0;
}

输入格式:

输入圆心和半径,x y r中间用空格分隔。

输出格式:

输出圆的周长,小数点后保留2位有效数字。

输入样例:

1 2 3

输出样例:

在这里给出相应的输出。例如:

Point constructor called
Circle constructor called
18.84
Circle destructor called
Point destructor called
#include <iostream>
#include<iomanip>
#include<cstdio>
using namespace std;
class Point
{
    protected:
    float x,y;
    public:
    Point(float x,float y)
    {
        this->x=x;
        this->y=y;
        cout<<"Point constructor called"<<endl;
    }
    ~Point()
    {
        cout<<"Point destructor called"<<endl;
    }
};
class Circle:public Point
{
    private:
    float r;
    public:
    Circle(float x,float y,float r):Point(x,y)
    {
        this->r=r;
        cout<<"Circle constructor called"<<endl;
    }
    float getCircumference()
    {
        return 2*r*3.14;
    }
    ~Circle()
    {
        cout<<"Circle destructor called"<<endl;
    }
};
int main()
{
    float x,y,r;
    cin>>x>>y>>r;
    Circle c(x,y,r);
    float a=c.getCircumference();
    printf("%.2f\n",c.getCircumference());
    return 0;
}

7-2 动物世界

分数 15

全屏浏览

切换布局

作者 陈晓梅

单位 广东外语外贸大学

补充程序 :

1、实现Mammal类的方法

2、由Mammal类派生出Dog类,在Dog类中增加itsColor成员(COLOR类型)

3、Dog类中增加以下方法:

constructors: Dog()、Dog(int age)、Dog(int age, int weight)、Dog(int age, COLOR color)、 Dog(int age, int weight, COLOR color)、~Dog()

accessors: GetColor()、SetColor()

Other methods: WagTail()、BegForFood() ,并实现以上这些方法 。

提示:类似Speak()、WagTail()这些动作,函数体可以是输出一句话。比如:Mammal is spaeking... , The Dog is Wagging its tail...

4、补充主函数的问号部分,并运行程序,检查输出是否合理。

enum COLOR{ WHITE, RED, BROWN, BLACK, KHAKI };

class Mammal
{
    public:
        //constructors
        Mammal();
        Mammal(int age);
        ~Mammal();
        
        //accessors
        int GetAge() const;
        void SetAge(int);
        int GetWeight() const;
        void SetWeight(int);
        
        //Other methods    
        void Speak() const;
        void Sleep() const;        
    protected:
        int itsAge;
        int itsWeight;
};

int main()
{
    Dog Fido;
    Dog Rover(5);
    Dog Buster(6, 8);
    Dog Yorkie(3, RED);
    Dog Dobbie(4, 20, KHAKI);
    Fido.Speak();
    Rover.WagTail();
    cout << "Yorkie is " << ?? << " years old." << endl;
    cout << "Dobbie    weighs " << ?? << " pounds." << endl;   
    return 0;
}

输入格式:

输出格式:

按照程序格式输出。

输入样例:

在这里给出一组输入。例如:

输出样例:

在这里给出相应的输出。例如:

Mammal is speaking...
The dog is wagging its tail...
Yorkie is 3 years old.
Dobbie weighs 20 pounds.

 

#include<iostream>
using namespace std;
enum COLOR{ WHITE, RED, BROWN, BLACK, KHAKI };

class Mammal
{
    public:
        Mammal(){
            SetAge(0);
            SetWeight(0);
        };
        Mammal(int age):itsAge(age){};
        ~Mammal(){
        };
        int GetAge() const{
            return itsAge;
        };
        void SetAge(int a){
            itsAge=a;
        };
        int GetWeight() const{
            return itsWeight;
        };
        void SetWeight(int a){
            itsWeight=a;
        };

        //Other methods     
        void Speak() const{
            cout<<"Mammal is speaking..."<<endl;
        };
        void Sleep() const;        
    protected:
        int itsAge;
        int itsWeight;
};
class Dog: public Mammal{
    private:
    COLOR itsColor;
    public:
        Dog(){
            SetAge(0);
            SetWeight(0);
            SetColor(RED);
        };
        Dog(int age){
            SetAge(age);
        };
        Dog(int age,int weight){
            SetAge(age);
            SetWeight(weight);
        };
        
        Dog(int age,COLOR color){
            SetAge(age);
            itsColor=color;
        };
        Dog(int age,int weight,COLOR color){
            SetAge(age);
            SetWeight(weight);
            itsColor=color;
        };
        ~Dog(){
        };
        COLOR GetColor(){
            return itsColor;
        };
        void SetColor(COLOR color){
            itsColor=color;
        };
        void WagTail(){
            cout<<"The dog is wagging its tail...\n";
        };
        void BegForFood();
};

int main()
{
    Dog Fido;
    Dog Rover(5);
    Dog Buster(6, 8);
    Dog Yorkie(3, RED);
    Dog Dobbie(4, 20, KHAKI);
    Fido.Speak();
    Rover.WagTail();
    cout << "Yorkie is " << Yorkie.GetAge() << " years old." << endl;
    cout << "Dobbie weighs " << Dobbie.GetWeight() << " pounds." << endl;   
    return 0;
}

7-3 学生CPP成绩计算

分数 24

全屏浏览

切换布局

作者 余春艳

单位 福州大学

给出下面的人员基类框架:

class Person
{

protected:

     string name;
     int age;

public:

     Person();      
     Person (string p_name, int p_age);
     void display () {cout<<name<<“:”<<age<<endl;}

};

建立一个派生类student,增加以下成员数据:

int ID;//学号
float cpp_score;//cpp上机成绩
float cpp_count;//cpp上机考勤
float cpp_grade;//cpp总评成绩
     //总评成绩计算规则:cpp_grade = cpp_score * 0.9 + cpp_count * 2;

增加以下成员函数:

student类的无参构造函数

student类的参数化构造函数//注意cpp_grade为上机成绩和考勤的计算结果

void print()//输出当前student的信息

                 //其中cpp_grade输出保留一位小数
                //输出格式为ID name cpp_grade

生成上述类并编写主函数,根据输入的学生基本信息,建立一个学生对象,计算其cpp总评成绩,并输出其学号、姓名、总评成绩。

输入格式: 测试输入包含若干测试用例,每个测试用例占一行(学生姓名 学号 年龄 cpp成绩 cpp考勤)。当读入0时输入结束,相应的结果不要输出。

输入样例:

Bob 10001 18 75.5 4

Mike 10005 17 95.0 5

0

输出样例:

10001 Bob 75.9

10005 Mike 95.5

 

 #include<string>
#include<iomanip>
#include <iostream>
 
using namespace std;
 class Person {
 
protected:
 
     string name;
     int age;
public:
 
     Person(){
	 };      
     Person (string p_name, int p_age){
     	name=p_name;
		 age=p_age; 
	 };
     void display () {cout<<name<<":"<<age<<endl;}
};
class Student:public Person{
	int ID;
float cpp_score;
float cpp_count;
float cpp_grade;
     
     public :
     	Student(){}
     	Student(string Name,int id,float a,float b){
     	 name=Name;
     	 ID=id;
		 cpp_score=a;
		 cpp_count=b; }
     	void print(){
     		cpp_grade=cpp_grade = cpp_score * 0.9 + cpp_count * 2;
     	cout<<ID<<" "<<name<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<cpp_grade<<endl;
     	
		 }
     	
};
int main()
{   int ID;
   string name;int age;
   float cpp_score;
float cpp_count;
	cin >> name ;
	while(name!="0"){
	cin	>> ID >>age>> cpp_score >>cpp_count;
    Student a(name,ID,cpp_score,cpp_count);
    a.print();
    cin >> name ;
	}
    return 0;
    
}

7-4 制作评分器

分数 24

全屏浏览

切换布局

作者 jolie

单位 佳木斯大学

本题目要编写一个评分器。创建考试后,当输入做题总数和错题数时自动求出分数
代码中包含两个类,分别是:
1、父类Grade负责将成绩分成级别,如,A:100-90;B:89-80;C:79-70;D:69-60;F:低于60分
私有成员:
字符变量letter---- 表示成绩的级别,如‘A’、‘B’等
单精度浮点变量score---表示真实分数
函数calcGrade()---无返回值,将分数分成级别
公有成员:
setScore函数--设置分数
getScore函数--获取分数
getLetter函数---获取成绩级别
2、子类Test 公有继承Grade,用以创建考试
私有成员:
变量numQuestions---- 表示做题总数
变量pointsEach---表示每题分数
变量numMissed()---表示做错题数
公有成员:
构造函数,用以初始化考试做题总数与错题数

输入格式:

总题数
错题数

输出格式:

The score is:分数
The grade is:级别

输入样例:

在这里给出一组输入。例如:

12
3

输出样例:

在这里给出相应的输出。例如:

The score is:75.00
The grade is:C
#include<iostream>
using namespace std;
 
class Grade
{
 
public:
    string letter;
    double score;
    Grade(int all, int p)
    {
        score = 100.0*(all-p)/all;
        calcGrade();
    }
    
    void calcGrade()
    {
        if(score>=90)
        {
            letter = 'A';
        }
        else if(score>=80 && score < 90)
        {
            letter = 'B';
        }
        else if(score>=70 && score <80)
        {
            letter = 'C';
        }
        else if(score >=60 && score <70)
        {
            letter = 'D';
        }
        else
        {
            letter = 'F';
        }
    }
    double getScore()
    {
        return score;
    }
    string getLetter()
    {
        return letter;
    }
};
int main()
{
    int np,p;
    cin>>np>>p;
    Grade* g= new Grade(np, p);
    printf("The score is:%.2f\n", g->getScore());
    cout<<"The grade is:"<<g->getLetter()<<endl;
    return 0;
}

7-5 多边形周长计算(继承)

分数 25

全屏浏览

切换布局

作者 余春艳

单位 福州大学

给出下面的多边形基类框架:

class polygon

{ protected:

   int number;//边数,最多不超过100条边

private:

   int side_length[100];//边长数组

public:

   polygon();//构造函数根据需要重载
   int perimeter();//计算多边形边长
   void display();//输出多边形边数和周长

}

建立一个派生类rectangle(矩形),增加以下数据成员:

  int height;
  int width;

增加以下成员函数:

 rectangle类的无参和参数化构造函数
 int perimeter();//计算矩形边长
 void display();//输出多边形边数和周长

建立一个派生类equal_polygon(等边多边形),增加以下数据成员:

  int side_len;

增加以下成员函数:

 equal_polygon类的无参和参数化构造函数
 int perimeter();//计算等边多边形边长
 void display();//输出多边形边数和周长

生成上述类并编写主函数,根据输入的多边形信息,相应建立一个多边形类对象或矩形类对象或等边多边形类对象,计算每一个多边形的周长并且输出其边数和周长。

输入格式: 测试输入包含一个测试用例,该测试用例的第一行输入多边形的个数n,接下来n行每一行给出一个多边形的基本信息,每行的第一个数字为当前多边形的类型,0为一般多边形,后面跟随m个数字为m条边的边长,-1为一般多边形边长输入结束标志,1为矩形,后面跟随两个数字,分别为height和width,2为等边多边形,后面跟随两个数字为等边多边形的边数和边长。

输入样例:

3

0 32 54 76 88 24 -1

1 32 54

2 3 32

输出样例:

5 274

4 172

3 96

 

 #include<string>
#include<iomanip>
#include <iostream>
 
using namespace std;
class polygon
 
{
    protected:
 
   int number;
private:
   int side_length[100];
public:
	polygon(){
	};
   polygon(int num){
   number=num;
   };      
   void set(int a,int b){
   	side_length[a]=b;
   }
  //构造函数根据需要重载
   int perimeter(){
   	int a=0;
      for(int i=0;i<number;i++){
      	a+=side_length[i];
	  }
	  return a;
   }//计算多边形边长
   void display(){
   printf("%d %d\n",number,this->perimeter());}//输出多边形边数和周长
};
class rectangle:public polygon{
	  int height;
  int width;
  public :
   rectangle(){};
    rectangle(int hei,int wid){
	height=hei;width=wid;}
	int perimeter(){
	  return 2*(width+height);
	}
 void display(){
     printf("4 %d\n",2*(width+height));
 }
	
   
};
class equal_polygon:public polygon{
	  int side_len;
	  public :
	  	  equal_polygon(){
			};
			 equal_polygon(int num,int side){
			 	side_len=side;
			 	number=num;
			 }
 int perimeter(){
 	return number*side_len;
 }//计算等边多边形边长
 void display(){
 printf("%d %d\n",number,number*side_len);}
int main()
{   
int n;
scanf("%d",&n);
int a;int array[100];
for(int i=0;i<n;i++){
	scanf("%d",&a);
	if(a==0){
	int b;
		scanf("%d",&b);int count=0;
 
		while(b!=-1){
		
			array[count++]=b;
			scanf("%d",&b);
		}
		polygon p(count);
		for(int j=0;j<count;j++){
			p.set(j,array[j]);
		}
	p.display();
	}
	if(a==1){
		int width,height;
		scanf("%d %d",&width,&height);
			rectangle r(height,width);
		r.display();
	}	
	if(a==2){
		int number,len;
		scanf("%d %d",&number,&len);
		equal_polygon e(number,len);
		e.display();
	}
}
    return 0;
    
}

7-6 两点间距离计算

分数 32

全屏浏览

切换布局

作者 余春艳

单位 浙江大学

给出下面的一个基类框架:

class Point_1D

{ protected:

    float x;//1D 点的x坐标

public:

    Point_1D(float p = 0.0);

    float distance(const Point_1D & p2);

}

以Point_1D为基类建立一个派生类Point_2D,增加一个保护数据成员:

    float y;//2D平面上点的y坐标

以Point_2D为直接基类再建立一个派生类Point_3D,增加一个保护数据成员:

    float z;//3D立体空间中点的z坐标

生成上述类并编写主函数,根据输入的点的基本信息,建立点对象,并能计算该点到原点的距离。

输入格式: 测试输入包含若干测试用例,每个测试用例占一行(点的类型(1表示1D点,2表示2D点,3表示3D点) 第一个点坐标信息(与点的类型相关) 第二个点坐标信息(与点的类型相关))。当读入0时输入结束,相应的结果不要输出。

输入样例:

1 -1 0

2 3 4 0 0

3 1 2 2 0 0 0

0

输出样例:

Distance from Point -1 to Point 0 is 1

Distance from Point(3,4) to Point(0,0) is 5

Distance from Point(3,3,3) to Point(0,0,0) is 3

 

 

#include<iostream>
#include<cmath>
using namespace std;
class Point_1D

{
protected:

	float x;

public:


	Point_1D(float a) {
		x = a;
	}
	float distance(const Point_1D& p1, const Point_1D& p2) {
		float d = 0.0;
		float t = 0.0;
		t = (p1.x - p2.x)* (p1.x - p2.x);

		d = sqrt(t);
		return d;
	}

};
class Point_2D :public  Point_1D {
protected:
	float y;//2D平面上点的y坐标
public:

	Point_2D(float a, float b) :Point_1D(a) {
		y = b;
	}
	float distance1(const Point_2D& p3, const Point_2D& p4) {
		float d = 0.0;
		float t = 0.0;
		t = (p3.x - p4.x) * (p3.x - p4.x) + (p3.y - p4.y) * (p3.y - p4.y);

		d = sqrt(t);
		return d;
	}
};
class Point_3D :public  Point_2D {
protected:
	float z
public:
	Point_3D(float a, float b, float c) :Point_2D(a, b) {
		z = c;
	}
	float distance2(const Point_3D& p5, const Point_3D& p6) {
		float d = 0.0;
		float t = 0.0;
		t = (p5.x - p6.x) * (p5.x - p6.x) + (p5.y- p6.y) * (p5.y - p6.y)+(p5.z - p6.z) * (p5.z - p6.z);
		d = sqrt(t);
		return d;
	}
};
int main() {
	int ch = 0;

	while (cin >> ch) {
		if (ch == 0)break;
		if (ch == 1) {
			float a,b;
			cin >> a>>b;
			Point_1D p1(a);
			Point_1D p2(b);
			
			cout << "Distance from Point " << a << " to Point "<<b<<" is " << p1.distance(p1,p2) << endl;
		}
		else if (ch == 2) {
			float a, b,c,d;
			cin >> a >> b>>c>>d;
			Point_2D p2(a, b);
			Point_2D p3(c, d);
			cout << "Distance from Point" << "(" << a << "," << b << ")" << " to Point("<<c<<","<<d<<") is " << p2.distance1(p2,p3) << endl;

		}
		else if (ch == 3) {
			float a, b, c,d,e,f;
			cin >> a >> b >> c>>d>>e>>f;
			Point_3D p3(a, b, c);
			Point_3D p4(d, e, f);
			cout << "Distance from Point" << "(" << a << "," << b << "," << c << ")" << " to Point("<<d<<","<<e<<","<<f<<") is " << p3.distance2(p3,p4) << endl;
		}
	}
	return 0;
}

7-7 日程安排(多重继承+重载)

分数 32

全屏浏览

切换布局

作者 余春艳

单位 福州大学

已有一个日期类Date,包括三个protected成员数据

int year;

int month;

int day;

另有一个时间类Time,包括三个protected成员数据

int hour;

int minute;

int second;

现需根据输入的日程的日期时间,安排前后顺序,为此以Date类和Time类为基类,建立一个日程类Schedule,包括以下新增成员:

int ID;//日程的ID

bool operator < (const Schedule & s2);//判断当前日程时间是否早于s2

生成以上类,并编写主函数,根据输入的各项日程信息,建立日程对象,找出需要最早安排的日程,并输出该日程对象的信息。

输入格式: 测试输入包含若干日程,每个日程占一行(日程编号ID 日程日期(****//)日程时间(::**))。当读入0时输入结束,相应的结果不要输出。

输入样例:

1 2014/06/27 08:00:01

2 2014/06/28 08:00:01

0

输出样例:

The urgent schedule is No.1: 2014/6/27 8:0:1

 

#include <iostream>
using namespace std;

class Date{
protected:
    int year;
    int month;
    int day;
public:
    
    Date(int y,int mon,int d):year(y),month(mon),day(d){}
    
    void showdate(){cout<<" "<<year<<"/"<<month<<"/"<<day;}
};
class Time{
protected:
    int hour;
    int minute;
    int second;
public:
    Time(int h,int min,int s):hour(h),minute(min),second(s){}
    void showtime(){cout<<" "<<hour<<":"<<minute<<":"<<second;}
};
class Schedule:public Date,public Time{
    int ID;
public:
    bool operator < (const Schedule & s2);
    Schedule(int id,int y,int mon,int d,int h,int min,int s):Date(y,mon,d),Time(h,min,s),ID(id){}
    void show();
};
bool Schedule::operator < (const Schedule & s2){
    if(year<s2.year)
        return 1;
    else if(year>s2.year)
        return 0;
    else{
        if(month<s2.month)
            return 1;
        else if(month>s2.month)
            return 0;
        else{
            if(day<s2.day)
                return 1;
            else if(day>s2.day)
                return 0;
            else {
                if(hour<s2.hour)
                    return 1;
                else if(hour>s2.hour)
                    return 0;
                else{
                    if(minute<s2.minute)
                        return 1;
                    else if(minute>s2.minute)
                        return 0;
                    else {
                        if(second<s2.second)
                            return 1;
                        else 
                            return 0;
                    }
                }
            }
        }
    }
}
void Schedule::show(){
    cout<<"No."<<ID<<":";
    Date::showdate();
    Time::showtime();
}
int main()
{
    int id,y,mon,d,h,min,s,i=0;
    Schedule s2(0,9999,9999,9999,999,999,99);
    while(1)
    {
        cin>>id;
        if(id==0)
            break;
        i++; 
        scanf("%d/%d/%d",&y,&mon,&d);
        scanf("%d:%d:%d",&h,&min,&s);
        Schedule s1(id,y,mon,d,h,min,s);
        if(s1<s2)
            s2=s1;
    }
    if(i!=0)
    {
        cout<<"The urgent schedule is ";
        s2.show();
    }
    return 0;
}

7-8 A是A1的虚基类

分数 24

全屏浏览

切换布局

作者 孙杰

单位 青岛大学

本题目要求读入3个整数A、B和C,然后按照下列要求完成相关设计:1.定义一个基类A,在其中包含保护的数据成员int i,设计类A的带参构造函数对i进行初始化,定义成员函数display()显示i值; 2.定义基类A的公有派生类A1,且A是A1的虚基类;A1中包含保护的数据成员int j,设计类A1的构造函数; 3.定义基类A的公有派生类A2,且A是A2的虚基类;A2中包含保护的数据成员int k,设计类A2的构造函数; 4.定义类A3,A3是A1和A2以多继承方式生成的公有派生类,设计类A3的构造函数;定义成员函数disp()在其中调用display()函数显示i值,另外输出j和k值; 5.在main()中定义类A3的1个对象变量,通过输入的3个整数完成对象的创建;调用类A3的成员函数disp()输出信息。

输入格式:

输入在一行中给出3个绝对值不超过1000的整数A、B和C。

输出格式:

按行输出每个类中的构造函数中的信息和在主函数中调用的对象的成员函数。

输入样例:

在这里给出一组输入。例如:

1 2 3

输出样例:

在这里给出相应的输出。例如:

Call A:i=1
Call A1:i=1
Call A2:i=1
Call A3:i=1
i=1
j=2
k=3

 

#include <iostream>
using namespace std;
class A{
	protected:
		int i;
	public :
    A(){
	}
	A(int a):i(a){
		printf("Call A:i=%d\n",i);
	}
	void display(){
		printf("i=%d\n",i);
	}
};
class A1:virtual public A{
	protected:
	int i1,j;
	public :
		 A1(){
	}
	A1(int a):j(a){
		i1=i;
		printf("Call A1:i=%d\n",i);
	} 
};
class A2:virtual public A{
	protected:
		int k;
		public :
			 A2(){
	}
			A2(int a):k(a){
 
				printf("Call A2:i=%d\n",i);
			}
};
class A3:protected A1,protected A2{
	public :
	
		 A3(){
	}
		A3(int a,int b,int c):A(a),A1(b),A2(c){
		printf("Call A3:i=%d\n",i);
		}
		void disp(){
			this->display();
			printf("j=%d\n",j);
			printf("k=%d\n",k);
		} 
};
int main(){
	int a,b,c;
	scanf("%d %d %d",&a,&b,&c);
	A3 A(a,b,c);
	A.disp();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值