C++习题-类04
来源:互联网 发布:布瑞克农业数据终端 编辑:程序博客网 时间:2024/05/18 01:28
/*
定义一个Point类,有私有数据成员X和Y是double型的;
由Point类的构造函数完成对X和Y的初始化;
有成员函数getX()可获取数据成员X的值、
getY()可获取数据成员Y的值。
由Point类公有派生出Rectangle类,
Rectangle类中新增私有数据成员W和H是double型的;
Rectangle类的构造函数能完成对所有数据成员的初始化;
*/
#include <iostream>
using namespace std;
class Point{
public:
Point();
Point(double x,double y);
double getX();
double getY();
private:
double X;
double Y;
};
class Rectangle:public Point{
public:
Rectangle();
Rectangle(double x,double y,double w,double h);
private:
double W;
double H;
};
int main()
{
Rectangle rect(1.0,2.0,3.0,4.0);
cout << "X=" << rect.getX() <<endl;
cout << "Y=" << rect.getY() <<endl;
return 0;
}
Point::Point(double x,double y){
X=x;
Y=y;
}
double Point::getX(){
return X;
}
double Point::getY(){
return Y;
}
Rectangle::Rectangle(double x,double y,double w,double h):Point(x,y){
W=w;
H=h;
}
定义一个Point类,有私有数据成员X和Y是double型的;
由Point类的构造函数完成对X和Y的初始化;
有成员函数getX()可获取数据成员X的值、
getY()可获取数据成员Y的值。
由Point类公有派生出Rectangle类,
Rectangle类中新增私有数据成员W和H是double型的;
Rectangle类的构造函数能完成对所有数据成员的初始化;
*/
#include <iostream>
using namespace std;
class Point{
public:
Point();
Point(double x,double y);
double getX();
double getY();
private:
double X;
double Y;
};
class Rectangle:public Point{
public:
Rectangle();
Rectangle(double x,double y,double w,double h);
private:
double W;
double H;
};
int main()
{
Rectangle rect(1.0,2.0,3.0,4.0);
cout << "X=" << rect.getX() <<endl;
cout << "Y=" << rect.getY() <<endl;
return 0;
}
Point::Point(double x,double y){
X=x;
Y=y;
}
double Point::getX(){
return X;
}
double Point::getY(){
return Y;
}
Rectangle::Rectangle(double x,double y,double w,double h):Point(x,y){
W=w;
H=h;
}
- 习题(C++) 04-7-15
- C++习题-类04
- C++primer习题编写Sales_item类
- C习题(team)
- c++primer 习题9.12
- c++primer 习题9.35
- c++primer 习题9.36
- c++primer 习题9.18
- c++primer 习题9.20
- c++primer 习题9.42
- c++primer 习题9.43
- c++primer 习题9.26
- 数据结构---习题(C++)
- C++Primer习题6.12
- C++primer习题6.20
- C++Primer习题9.26
- C++Primer 习题9.38
- C++Primer 习题9.39
- Linux L3 驱动
- NYOJ - 兔子的烦恼(一)
- C++习题-类03
- C++习题-类02
- C#与Java异同
- C++习题-类04
- AngularJS介绍
- static关键字
- Windows Store apps开发[83]Windows 8 开发31日-第25日-加速度计
- Android OnTouchListener实时监听触点坐标,需要同时监听OnClickListener才有效
- ODE 参考手册
- linux驱动学习笔记(linux驱动头文件说明)
- 简单过河卒问题
- 漫长的战斗