#include<iostream>
#include<Cmath>
using namespace std;
class Point //定义坐标点类
{
public:
Point():x(0),y(0) {};
Point(double x0, double y0):x(x0), y(y0) {};
void PrintPoint(); //输出点的信息
double x,y; //数据成员,表示点的横坐标和纵坐标
};
void Point::PrintPoint()
{
cout<<"Point:("<<x<<","<<y<<")"; //输出点
}
class Line: public Point //利用坐标点类定义直线类, 其基类的数据成员表示直线的中点
{
public:
Line(Point pt1, Point pt2); //构造函数,初始化直线的两个端点及由基类数据成员描述的中点,中点的坐标可以由端点坐标计算而来
double Length(); //计算并返回直线的长度
void PrintLine(); //输出直线的两个端点和直线长度
void showmiddlepoint();
private:
Point pts,pte; //直线的两个端点
};
//下面定义Line类的成员函数
Line::Line(Point pt1,Point pt2):pts(pt1),pte(pt2){}//此处不加基类的构造函数,执行基类的默认构造函数。下同。
double Line::Length(){
return sqrt((pts.x-pte.x)*(pts.x-pte.x)+(pts.y-pte.y)*(pts.y-pte.y));
}
void Line::PrintLine(){
cout<<"这两个端点分别是:";
pts.PrintPoint();pte.PrintPoint();
cout<<endl<<"线段的长度是:"<<Length()<<endl;
}
void Line::showmiddlepoint(){
cout<<"MiddlePoint:("<<(pts.x+pte.x)/2<<","<<(pts.y+pte.y)/2<<")";
}
int main()
{
Point ps(-1,-1),pe(1,1);
Line l(ps,pe);
// _____________________;//输出直线l的信息(请补全代码)
l.PrintLine();
cout<<"\n The middle point of Line: ";
//_____________________ ;//输出直线l中点的信息(请补全代码,调用成员函数完成)
l.showmiddlepoint();
return 0;
}
【项目2】要求与项目1几乎完全相同(除Point类中的数据成员要求为private型),要求补全点类和直线类的定义,调用main()函数输出一条直线的信息。
请不要直接copy项目1的代码,通过修改项目1的程序完成项目2的要求。在有关“抄之有道”的学习方法中,我们有阅读代码,然后自己再写出来的建议。所以,项目2可以在以下代码的提示下开始去做,在项目1中获得的经验会有所帮助,这个过程也会对项目1中的收获有更多理解。其实,当这一些技术熟练之后,根据需求会自己设计并实现,那是我们的最终目标。
项目2可以参考的代码如下,在完成时可以根据需要自行添加其他成员函数,成员函数在类内实现还是类外实现,也可以自行决定。
- class Point //定义坐标点类
- {
- public:
- //定义必要的构造函数
- void PrintPoint(); //输出点的信息
- private:
- double x,y; //点的横坐标和纵坐标
- };
- class Line: public Point //利用坐标点类定义直线类, 其基类的数据成员表示直线的中点
- {
- public:
- Line(Point pts, Point pte); //构造函数,用初始化直线的两个端点及由基类数据成员描述的中点
- void PrintLine(); //输出直线的两个端点和直线长度
- private:
- class Point pts, pte; //直线的两个端点
- };
- int main()
- {
- Point ps(-2,5),pe(7,9);
- Line l(ps,pe);
- //下面输出直线l的端点、长度和、中点的信息
- return 0;
- }
下为自己的代码
#include<iostream>
#include<cmath>
using namespace std;
class Point //定义坐标点类
{
public:
Point(){x=0;y=0;}//这一句非常重要 保证Line的构造函数里面的赋值有意义
Point(double xx,double yy):x(xx),y(yy){}//定义必要的构造函数
void PrintPoint(); //输出点的信息
double getx(){return x;}
double gety(){return y;}
private:
double x,y; //点的横坐标和纵坐标
};
class Line: public Point //利用坐标点类定义直线类, 其基类的数据成员表示直线的中点
{
public:
Line(Point ps,Point pe){pts=ps;pte=pe;} //构造函数,用初始化直线的两个端点及由基类数据成员描述的中点
double length();
void PrintLine(); //输出直线的两个端点和直线长度
private:
Point pts, pte; //直线的两个端点.....可以不加class吗?
};
void Point::PrintPoint(){
cout<<"这个点的坐标是:("<<x<<","<<y<<")";
}
double Line::length(){
return sqrt((pts.getx()-pte.getx())*(pts.getx()-pte.getx())+(pts.gety()-pte.gety())*(pts.gety()-pte.gety()));
}
void Line::PrintLine(){
cout<<"端点坐标是:";
pts.PrintPoint();pte.PrintPoint();
cout<<"直线的长度是:"<<length();
}
int main()
{
Point ps(-2,5),pe(7,9);
Line l(ps,pe);
//下面输出直线l的端点、长度和、中点的信息
l.PrintLine();
return 0;
}
【项目3】定义一个名为CPerson的类,有以下私有成员:姓名、身份证号、性别和年龄,成员函数:构造函数、析构函数、输出信息的函数。并在此基础上派生出CEmployee类,派生类CEmployee增加了两个新的数据成员,分别用于表示部门和薪水。要求派生类CEmployee的构造函数显示调用基类CPerson的构造函数,并为派生类CEmployee定义析构函数,定义输出信息的函数。
#include <iostream>
#include<iomanip>
using namespace std;
class CPerson
{
protected:
char *m_szName;
char *m_szId;
int m_nSex;//0:women,1:man
int m_nAge;
public:
CPerson(char *name,char *id,int sex,int age);
void Show1();
~CPerson(); //需要释放建立对象时动态分配的内存
};
CPerson::CPerson(char *name,char *id,int sex,int age){
m_szName=new char[strlen(name)+1];
strcpy(m_szName,name);
m_szId=new char[strlen(id)+1];
strcpy(m_szId,id);
m_nSex=sex;
m_nAge=age;
}
void CPerson::Show1(){
cout<<setw(10)<<"姓名"<<setw(10)<<"id"<<setw(10)<<"性别"<<setw(10)<<"年龄"<<endl;
cout<<setw(10)<<m_szName<<setw(10)<<m_szId<<setw(10)<<m_nSex<<setw(10)<<m_nAge<<endl;
}
CPerson::~CPerson(){delete []m_szName;delete []m_szId;}
class CEmployee:public CPerson
{
private:
char *m_szDepartment;
float m_Salary;
public:
CEmployee(char *name,char *id,int sex,int age,char *department,float salary);
void Show2();
~CEmployee();
};
CEmployee::CEmployee(char *name,char *id,int sex,int age,char *department,float salary):CPerson(name,id,sex,age){
m_szDepartment=new char[strlen(department)+1];
strcpy(m_szDepartment,department);
m_Salary=salary;
}
void CEmployee::Show2(){
cout<<setw(10)<<"姓名"<<setw(10)<<"id"<<setw(10)<<"性别"<<setw(10)<<"年龄"<<setw(10)<<"部门"<<setw(10)<<"工资"<<endl;
cout<<setw(10)<<m_szName<<setw(10)<<m_szId;if(m_nSex==0)cout<<setw(10)<<"woman";else cout<<"man";cout<<setw(10)<<m_nAge<<setw(10)<<m_szDepartment<<setw(10)<<m_Salary<<endl;
}
CEmployee::~CEmployee(){delete []m_szDepartment;}
int main()
{
char name[10],id[19],department[10];
int sex,age;
float salary;
cout<<"input employee's name,id,sex(0:women,1:man),age,department,salary:\n";
cin>>name>>id>>sex>>age>>department>>salary;
CEmployee employee1(name,id,sex,age,department,salary);
employee1.Show2();
return 0;
}
【项目4】按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,并且完成要求的计算任务:
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员 (半径);
(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。
要求编写程序,设计出各类中基本的成员函数(包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等),使之能用于处理以上类对象,最后求出圆格柱体的表面积、体积并输出。
(提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的项目)
#include <iostream>
using namespace std;
#define pi 3.1415926
class Point{
public:
Point():x(0),y(0){};
Point(double xx,double yy):x(xx),y(yy){};
~Point(){};
double GetX(){return x;}
double GetY(){return y;}
private:
double x,y;
};
class Circle:public Point{
public:
Circle():midp(0,0),radius(0){};
Circle(Point p,double rad):midp(p),radius(rad){};
~Circle(){};
double Area();
double Permeter();
void showmiddlepoint();
private:
Point midp;
double radius;
};
class Cylinder:public Circle{
public:
Cylinder():c(),higth(0){}
Cylinder(Circle c1,double h):c(c1),higth(h){}
~Cylinder(){}
double Volume();
double Area();
private:
Circle c;
double higth;
};
double Cylinder::Volume(){
return c.Area()*higth;
}
double Cylinder::Area(){
return c.Permeter()*higth+2*c.Area();
}
double Circle::Area(){return pi*radius*radius;}
double Circle::Permeter(){return 2*pi*radius;}
void Circle::showmiddlepoint(){cout<<"圆的中点是:("<<midp.GetX()<<","<<midp.GetY()<<")"<<endl;}
int main()
{
Point p1(1,1);
Circle c(p1,1);
cout<<"area is:"<<c.Area()<<" permeter is"<<c.Permeter()<<endl;
c.showmiddlepoint();
Cylinder cy(c,5);
cout<<"the cylinder area is:"<<cy.Area()<<" volume is"<<cy.Volume()<<endl;
return 0;
}
3937


&spm=1001.2101.3001.11974&articleId=9942323&d=1&t=3&u=ef604f88fb674fb08c804c5613a07ff8)

被折叠的 条评论
为什么被折叠?



