/*继承与派生1
请以点类Point为基类派生出一个圆类Circle。
圆类Circle的数据成员为r(私有属性,存储圆的半径,圆心的点坐标通过继承点类Point加以实现),
成员函数有构造函数Circle、计算圆的面积函数Area、计算圆的周长函数Perimeter和输出函数Display,
其中构造函数实现基类和圆类的数据成员的初始化,
Display函数实现圆心坐标(利用基类Point的Display实现)、圆的半径、圆的面积(利用Area函数实现)和圆的周长(利用Perimeter函数实现)的输出。
请编写圆类的定义及成员函数实现,并在主函数中定义圆类对象,验证各个函数的正确性。
说明:圆周率PI的取值为3.14
已知Point类的定义及main代码如下:(不允许改动)
#include<iostream>
using namespace std;
const double Pi = 3.14;
class Point
{
public:
Point(double xx, double yy);
void Display();
private:
double x, y;
};
Point::Point(double xx, double yy)
{
x = xx;
y = yy;
}
void Point::Display()
{
cout << "Center:Point(" << x << "," << y << ")" << endl;
}
class Circle:public Point
{
public:
Circle(double xx,double yy, double r):Point(xx,yy)
{
c_r = r;
}
double Area()
{
return c_r * c_r * Pi;
}
double Perimeter()
{
return c_r * Pi * 2;
}
void Display()
{
Point::Display();
cout << "Radius:"<<c_r << endl;
cout << "Area:"<< Area() << endl;
cout << "Perimeter:"<< Perimeter() << endl;
}
private:
double c_r;
};
int main()
{
double x, y, r;
cin >> x >> y >> r;
Circle C(x, y, r);
C.Display();
return 0;
}
/*继承与派生2
1.Person类派生大学生CollegeStu类(1)。设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,
编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;
由Person类派生出大学生类CollegeStu,其属性有专业major(指针类型),C++程序设计课程成绩score(double型),
编写构造函数(实现数据初始化)、输出函数Display(包括name,id,major,score)。main的代码如下:(不允许改动)
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
public:
Person(char *name,int id)
{
int a = strlen(name);
m_name = new char[a+2];
int i;
for (i = 0; i < a; i++)
{
*(m_name+i) = *(name+i);
}
*(m_name + i + 1) = '\0';
m_id = id;
}
void Display()
{
cout << "Name:" << m_name << endl;
cout << "ID:" << m_id << endl;
}
~Person()
{
if (m_name != NULL)
{
delete[] m_name;
m_name = NULL;
}
}
private:
char* m_name;
int m_id;
};
class CollegeStu :public Person
{
public:
CollegeStu(char *name,int id,char *major,double score):Person(name,id)
{
int a = strlen(major);
m_major = new char[a+2];
int i;
for (i = 0;i < a; i++)
{
*(m_major+i) = *(major+i);
}
*(m_major + i+1 ) = '\0';
m_score = score;
}
void Display()
{
Person::Display();
cout << "Major:"<< m_major<<endl;
cout << "C++ Score:"<< m_score<<endl;
}
~CollegeStu()
{
if (m_major != NULL)
{
delete[] m_major;
m_major = NULL;
}
}
private:
char* m_major;
double m_score;
};
int main()
{
char name[81], major[81];
int id;
double score;
cin >> name >> id >> major >> score;
CollegeStu cs(name, id, major, score);
cs.Display();
return 0;
}
/*继承与派生3
Person类派生大学生CollegeStu类(2)。
设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,
编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;
由Person类派生出大学生类CollegeStu,其属性有专业major(指针类型),C++程序设计课程成绩score(double型),
编写构造函数(实现数据初始化)、输出函数Display(只输出major,score)。main的代码如下:(不允许改动)
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
public:
Person(char *name,int id)
{
int a = strlen(name);
m_name = new char[a+1];
for (int i = 0; i < a; i++)
{
*(m_name+i) = *(name+i);
}
m_id = id;
}
void Display()
{
cout << "Name:" << m_name << endl;
cout << "ID:" << m_id << endl;
}
~Person()
{
if (m_name != NULL)
{
delete[] m_name;
m_name = NULL;
}
}
private:
char* m_name;
int m_id;
};
class CollegeStu :public Person
{
public:
CollegeStu(char *name,int id,char *major,double score):Person(name,id)
{
int a = strlen(major);
m_major = new char[a+1];
for (int i = 0;i < a; i++)
{
*(m_major+i) = *(major+i);
}
m_score = score;
}
void Display()
{
cout << "Major:"<< m_major<<endl;
cout << "C++ Score:"<< m_score<<endl;
}
~Co