1197: 继承与派生1
Description
请以点类Point为基类派生出一个圆类Circle。圆类Circle的数据成员为r(私有属性,存储圆的半径,圆心的点坐标通过继承点类Point加以实现),成员函数有构造函数Circle、计算圆的面积函数Area、计算圆的周长函数Perimeter和输出函数Display,其中构造函数实现基类和圆类的数据成员的初始化,Display函数实现圆心坐标(利用基类Point的Display实现)、圆的半径、圆的面积(利用Area函数实现)和圆的周长(利用Perimeter函数实现)的输出。请编写圆类的定义及成员函数实现,并在主函数中定义圆类对象,验证各个函数的正确性。
说明:圆周率PI的取值为3.14
已知Point类的定义及main代码如下:(不允许改动)
class Point
{
public:
Point(double xx,double yy); //constructor
void Display(); //display point
private:
double x,y; //平面的点坐标x,y
};
int main()
{
double x,y,r;
cin>>x>>y>>r; //圆心的点坐标及圆的半径
Circle C(x,y,r);
C.Display(); //输出圆心点坐标,圆的半径,圆的面积,圆的周长
return 0;
}
Input
Output
Sample Input
1.5 2.6 1.8
Sample Output
Center:Point(1.5,2.6)
Radius:1.8
Area:10.1736
Perimeter:11.304
#include<iostream>
using namespace std;
class Point
{
private:
double x; //平面的点坐标x
double y; //平面的点坐标y
public:
Point(double xx,double yy); //constructor
void Display();//display point
};
Point::Point(double xx,double yy) //constructor
{x=xx;y=yy;}
void Point::Display()//display point
{cout<<"Point("<<x<<","<<y<<")"<<endl;}
class Circle:public Point
{
private:
double r;
public:
Circle(double xx,double yy,double rr);
double Area();
double Perimeter();
void Display();
};
Circle::Circle(double xx,double yy,double rr):Point(xx,yy)
{r=rr;}
double Circle::Area()
{return 3.14*r*r;}
double Circle::Perimeter()
{return 2*3.14*r;}
void Circle::Display()
{
cout<<"Center:";
Point::Display();
cout<<"Radius:"<<r<<endl;
cout<<"Area:"<<Area()<<endl;
cout<<"Perimeter:"<<Perimeter()<<endl;
}
int main()
{
double x,y,r;
cin>>x>>y>>r; //圆心的点坐标及圆的半径
Circle C(x,y,r);
C.Display(); //输出圆心点坐标,圆的半径,圆的面积,圆的周长
return 0;
}
1217: 继承与派生2
Description
Person类派生大学生CollegeStu类(1)。设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业subject(指针类型),C++程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数Display(包括name,id,subject,score)。main的代码如下:(不允许改动)
int main()
{
char name[81],subject[81];
int id;
double score;
cin>>name>>id>>subject>>score;
CollegeStu cs(name,id,subject,score);
cs.Display();
return 0;
}
Input
Output
Sample Input
Zhangsan 2 Computer 89.5
Sample Output
Name:Zhangsan
ID:2
Subject:Computer
C++ Score:89.5
**************************************************************************
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
private:
char * name;
int id;
public:
Person()
{
name=NULL;
id=0;
}
Person(char *name1,int id1)
{
name=new char[strlen(name1)+1];
strcpy(name,name1);
id=id1;
}
~Person()
{
delete [] name;
}
void Display()
{
cout<<"Name:"<<name<<endl;
cout<<"ID:"<<id<<endl;
}
};
class Collegestu : public Person
{
private:
char * subject;
double score;
public:
Collegestu()
{
subject=NULL;
score=0;
}
Collegestu(char * name1,int id1,char * subject1,double score1):Person(name1,id1)
{
subject=new char [strlen(subject1)+1];
strcpy(subject,subject1);
score=score1;
}
~Collegestu()
{
delete [] subject;
}
void Display()
{
Person::Display();
cout<<"Subject:"<<subject<<endl;
cout<<"C++ Score:"<<score<<endl;
}
};
int main()
{
char name[81],subject[81];
int id;
double score;
cin>>name>>id>>subject>>score;
Collegestu cs(name,id,subject,score);
cs.Display();
return 0;
}
【附加题】在【1217: 继承与派生2】原题基础上增加拷贝构造函数
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
private:
char * name;
int id;
public:
Person()
{
name=NULL;
id=0;
}
Person(char *name1,int id1)
{
name=new char[strlen(name1)+1];
strcpy(name,name1);
id=id1;
}
Person(Person &p)
{
name=new char[strlen(p.name)+1];
strcpy(name,p.name);
id=p.id;
}
~Person()
{
delete [] name;
}
void Display()
{
cout<<"Name:"<<name<<endl;
cout<<"ID:"<<id<<endl;
}
};
class Collegestu : public Person
{
private:
char * subject;
double score;
public:
Collegestu()
{
subject=NULL;
score=0;
}
Collegestu(char * name1,int id1,char * subject1,double score1):Person(name1,id1)
{
subject=new char [strlen(subject1)+1];
strcpy(subject,subject1);
score=score1;
}
Collegestu(Collegestu &c):Person(c)
{
subject=new char [strlen(c.subject)+1];
strcpy(subject,c.subject);
score=c.score;
}
~Collegestu()
{
delete [] subject;
}
void Display()
{
Person::Display();
cout<<"Subject:"<<subject<<endl;
cout<<"C++ Score:"<<score<<endl;
}
};
int main()
{
char name[81],subject[81];
int id;
double score;
cin>>name>>id>>subject>>score;
Collegestu cs(name,id,subject,score);
Collegestu a=cs;
a.Display();
cs.Display();
return 0;
}
1218: 继承与派生3
Description
Person类派生大学生CollegeStu类(2)。设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业subject(指针类型),C++程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数Display(只输出subject,score)。main的代码如下:(不允许改动)
int main()
{
char name[81],subject[81];
int id;
double score;
cin>>name>>id>>subject>>score; //输入学生的姓名、id号、专业、成绩
CollegeStu cs(name,id,subject,score);
cs.Person::Display(); //输出姓名,id
cs.Display(); //输出专业、成绩
return 0;
}
Input
Output
Sample Input
Lixu 5 Software 87.5
Sample Output
Name:Lixu
ID:5
Subject:Software
C++ Score:87.5
**************************************************************************
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
private:
char * name;
int id;
public:
Person()
{
name=NULL;
id=0;
}
Person(char *name1,int id1)
{
name=new char[strlen(name1)+1];
strcpy(name,name1);
id=id1;
}
~Person()
{
delete [] name;
}
void Display()
{
cout<<"Name:"<<name<<endl;
cout<<"ID:"<<id<<endl;
}
};
class CollegeStu : public Person
{
private:
char * subject;
double score;
public:
CollegeStu()
{
subject=NULL;
score=0;
}
CollegeStu(char * name1,int id1,char * subject1,double score1):Person(name1,id1)
{
subject=new char [strlen(subject1)+1];
strcpy(subject,subject1);
score=score1;
}
~CollegeStu()
{
delete [] subject;
}
void Display()
{
cout<<"Subject:"<<subject<<endl;
cout<<"C++ Score:"<<score<<endl;
}
};
int main()
{
char name[81],subject[81];
int id;
double score;
cin>>name>>id>>subject>>score; //输入学生的姓名、id号、专业、成绩
CollegeStu cs(name,id,subject,score);
cs.Person::Display(); //输出姓名,id
cs.Display(); //输出专业、成绩
return 0;
}
1219: 继承与派生4
Description
已知Base为基类,派生出Derived类,两个类的定义及main的代码如下(不允许改动),请完成Base类和Derived类的构造函数和析构函数,能够根据输入获取相应的输出。
class Base
{
private:
int b;
public:
Base(int);
~Base();
};
class Derived:public Base
{
private:
int d;
public:
Derived(int,int);
~Derived();
};
int main()
{
int a,b;
cin>>a>>b;
Derived dr(a,b);
return 0;
}
Input
Output
Sample Input
1 3
Sample Output
Base 1 says hello
Derived 3 says hi
Derived 3 says bye
Base 1 says goodbye
**************************************************************************
#include<iostream>
using namespace std;
class Base
{
private:
int b;
public:
Base(int c)
{
b=c;
cout<<"Base "<<b<<" says hello"<<endl;
}
~Base()
{
cout<<"Base "<<b<<" says goodbye"<<endl;
}
};
class Derived:public Base
{
private:
int d;
public:
Derived(int c,int b):Base(c)
{
d=b;
cout<<"Derived "<<d<<" says hi"<<endl;
}
~Derived()
{
cout<<"Derived "<<d<<" says bye"<<endl;
}
};
int main()
{
int a,b;
cin>>a>>b;
Derived dr(a,b);
return 0;
}
1220: 继承与派生5
Description
由Array类派生出有序数组SortArray类,SortArray类中实现有序数组的插入。
已知Array类的定义如下(不允许增加成员函数):
class Array
{
public:
Array(); //构造函数,初始化为空数组(length置为0)
int Length(); //获取数组的实际长度
double Get(int pos); //获取data中下标为pos的元素的值
void Insert(int pos, double x); //在下标pos处插入x
void Display(); //输出线性表
private:
double data[MaxSize]; //存储元素(MaxSize为常量)
int length; //数组的实际长度
};
SortArray类定义如下(不允许增加成员函数):
class SortArray:private Array
{
public:
SortArray();
int Length(); //获取数组的实际长度
double Get(int pos); //获取data中下标为pos的元素的值
void Display(); //输出线性表
void Insert(double x); //递增有序数组中插入x,使序列仍有序
};
请实现Array类和SortArray类的成员函数, main中输入若干个实数,以0结束,利用SortArray类中的Insert函数将它们插入data中,得到有序序列,再利用Display函数输出有序序列。代码如下(不允许修改):
int main()
{
SortArray sa;
double num;
while(1)
{
cin>>num;
if(fabs(num)<=1e-6) break;
try
{
sa.Insert(num); //
}
catch(char* message)
{
cout <<message<<endl; //如失败提示失败信息
}
}
sa.Display();
return 0;
}
Input
Output
Sample Input
2.5 6.7 8.3 2.8 6.53 6.82 7.33 0
Sample Output
The length:7
The elements:2.5 2.8 6.53 6.7 6.82 7.33 8.3
**************************************************************************
#include <iostream>
#include <cmath>
using namespace std;
const int MaxSize=100; //顺序表的最大长度
class Array
{
public:
Array(); //构造函数,初始化为空数组(length置为0)
int Length(); //获取顺序表实际长度
double Get(int pos); //获取下标为pos的元素的值
void Insert(int pos, double x); //在下标pos处插入x
void Display(); //输出线性表
private:
double data[MaxSize]; //存储元素
int length; //数组的实际长度
};
Array::Array()
{ length=0;}
int Array::Length()
{ return length;}
double Array::Get(int pos)
{
if (pos<0 || pos>length-1) //下标不合法
throw "Illegal position";
return data[pos];
}
void Array::Insert(int pos, double x) //在下标pos处插入x
{
int i;
if (length>=MaxSize) //表满不能插入
throw "Overflow";
if (pos<0 ||pos>length) //下标不合法
throw "Illegal position";
for (i=length-1;i>=pos;i--) //将下标大于等于pos的元素后移
data[i+1]=data[i];
data[pos]=x; //在下标pos处插入元素x
length++; //线性表长度增1
}
void Array::Display() //输出线性表
{
int i;
cout<<"The length:"<<length<<endl;
cout<<"The elements:";
for (i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}
//class SortArray
class SortArray:private Array
{
public:
SortArray();
int Length();
double Get(int pos);
void Display();
void Insert(double x); //递增有序数组中插入x,使序列仍有序
};
SortArray::SortArray():Array(){}
int SortArray::Length()
{ return Array::Length();}
double SortArray::Get(int pos)
{ return Array::Get(pos);}
void SortArray::Display()
{ Array::Display();}
void SortArray::Insert(double x)//insert
{
int i;
if(Length()>=MaxSize) throw"Overflow";
for(i=0;i<Length();i++)
if(Get(i)>x)
break;
Array::Insert(i,x);
}
int main()
{
SortArray sa;
double num;
while(1)
{
cin>>num;
if(fabs(num)<=1e-6) break;
try
{
sa.Insert(num); //
}
catch(char* message)
{
cout <<message<<endl; //如失败提示失败信息
}
}
sa.Display();
return 0;
}
1221: 继承与派生6
Description
已知Array类的定义如下(不允许增加成员函数):
class Array
{
public:
Array(int size);
//构造函数,初始化数据成员(为data分配内存,MaxSize置为size,length置为0)
int Length(); //获取顺序表实际长度
double Get(int pos); //获取下标为pos的元素的值
void Insert(int pos, double x); //在下标pos处插入x
void Display(); //输出线性表
private:
double *data; //存储元素
int MaxSize;
int length; //数组的实际长度
};
SortArray类定义如下(不允许增加其它成员函数):
class SortArray:private Array
{
public:
SortArray(int size);
int Length(); //获取顺序表实际长度
double Get(int pos); //获取下标为pos的元素的值
void Display(); //输出线性表
void Insert(double x); //递增有序数组中插入x,使序列仍有序
};
main中的代码如下(不允许改动):
int main()
{
int size;
cin>>size;
SortArray sa(size);
double num;
while(1)
{
cin>>num;
if(fabs(num)<=1e-6) break;
try
{
sa.Insert(num);
}
catch(char* wrong)
{
cout <<wrong<<endl; //如失败提示失败信息
}
}
sa.Display();
return 0;
}
请实现Array类和SortArray类的成员函数。
Input
Output
Sample Input
20 2.5 6.7 8.3 2.8 6.53 6.82 7.33 0
Sample Output
The length:7
The elements:2.5 2.8 6.53 6.7 6.82 7.33 8.3
**************************************************************************
#include <iostream>
#include <cmath>
using namespace std;
class Array
{
public:
Array(int size); //构造函数,初始化数据成员(为data分配内存,MaxSize置为size,length置为0)
int Length(); //获取顺序表实际长度
double Get(int pos); //获取下标为pos的元素的值
void Insert(int pos, double x); //在下标pos处插入x
void Display(); //输出线性表
private:
double *data; //存储元素
int MaxSize;
int length; //数组的实际长度
};
Array::Array(int size)
{
MaxSize=size;
data=new double[MaxSize];
length=0;
}
int Array::Length()
{ return length;}
double Array::Get(int pos)
{
if (pos<0 || pos>length-1) //下标不合法
throw "Illegal position";
return data[pos];
}
void Array::Insert(int pos, double x) //在下标pos处插入x
{
int i;
if (length>=MaxSize) //表满不能插入
throw "Overflow";
if (pos<0 ||pos>length) //下标不合法
throw "Illegal position";
for (i=length-1;i>=pos;i--) //将下标大于等于pos的元素后移
data[i+1]=data[i];
data[pos]=x; //在下标pos处插入元素x
length++; //线性表长度增1
}
void Array::Display() //输出线性表
{
int i;
cout<<"The length:"<<length<<endl;
cout<<"The elements:";
for (i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}
class SortArray:private Array
{
public:
SortArray(int size);
int Length(); //获取顺序表实际长度
double Get(int pos); //获取下标为pos的元素的值
void Display(); //输出线性表
void Insert(double x); //递增有序数组中插入x,使序列仍有序
};
SortArray::SortArray(int size):Array(size){}
int SortArray::Length()
{ return Array::Length();}
double SortArray::Get(int pos)
{ return Array::Get(pos);}
void SortArray::Display()
{ Array::Display();}
void SortArray::Insert(double x)//insert
{
int i;
if(Length()>=MaxSize) throw"Overflow";
for(i=0;i<Length();i++)
if(Get(i)>x)
break;
Array::Insert(i,x);
}
int main()
{
int size;
cin>>size;
SortArray sa(size);
double num;
while(1)
{
cin>>num;
if(fabs(num)<=1e-6) break;
try
{
sa.Insert(num);
}
catch(char* wrong)
{
cout <<wrong<<endl; //如失败提示失败信息
}
}
sa.Display();
return 0;
}
1223: 继承与派生7
Description
已知由Automobille类派生出Car类和Wagon类,而后两者共同派生出StationWagon类,各类的定义及main中的代码(不允许改动)如下,请实现各个类的成员函数,完成相应的输出:
class Automobile //汽车类
{
private:
int power; //马力
public:
Automobile(int p);
void Display();
};
class Car:virtual public Automobile //小客车类
{
private:
int seat; //座位
public:
Car(int p,int s);
void Display();
};
class Wagon:virtual public Automobile //小货车类
{
private:
int load; //装载量
public:
Wagon(int p,int l);
void Display();
};
class StationWagon :public Car, public Wagon //客货两用车类
{
public:
StationWagon(int p, int s,int l);
void Display();
};
int main()
{
int power,load,seat;
cin>>power>>seat>>load;
StationWagon sw(power,seat,load);
sw.Display();
return 0;
}
Input
Output
Sample Input
108 3 10
Sample Output
StationWagon:
Power:108
Seat:3
Load:10
**************************************************************************
#include<iostream>
using namespace std;
class Automobile //汽车类
{
private:
int power; //马力
public:
Automobile(int p=0)
{ power=p; }
void Display()
{ cout<<"Power:"<<power<<endl; }
};
class Car:virtual public Automobile //小客车类
{
private:
int seat; //座位
public:
Car(int p=0,int s=0):Automobile(p)
{ seat=s; }
void Display()
{ cout<<"Seat:"<<seat<<endl; }
};
class Wagon:virtual public Automobile //小货车类
{
private:
int load; //装载量
public:
Wagon(int p=0,int l=0):Automobile(p)
{ load=l; }
void Display()
{ cout<<"Load:"<<load<<endl; }
};
class StationWagon :public Car, public Wagon //客货两用车类
{
public:
StationWagon(int p=0, int s=0,int l=0):Automobile(p),Car(p,s),Wagon(p,l){}
void Display()
{
cout<<"StationWagon:"<<endl;
Automobile::Display();
Car::Display();
Wagon::Display();
}
};
int main()
{
int power,load,seat;
cin>>power>>seat>>load;
StationWagon sw(power,seat,load);
sw.Display();
return 0;
}