【描述】
声明并实现一个Point类,表示直角坐标系中的一个点。Point类包括:
double类型的私有数据成员x和y,表示坐标。
无参(默认)构造函数,将坐标设置为原点。
有参构造函数,将坐标设置为给定的参数。
访问器函数getX和getY,分别用于访问点的x坐标和y坐标。
【输入】
0,0 4,5
【输出】
(0,0)
(4,5)
【来源】
《程序设计基础——以C++为例》第5章实验1。
#include <iostream>
using namespace std;
/* 请在此处编写Point类 */
class Point//Point类
{
private: //私有的
double x;//数据成员x和y
double y;
public:
Point(double x=0,double y=0)//有参构造函数,无参时默认x=0,y=0
{
this->x=x;
this->y=y;
}
//因为成员变量是private私有的,所以需要通过访问器间接访问
double getX()//访问点的x坐标和y坐标
{
return this->x;
}
double getY()
{
return this->y;
}
};
int main() {
double x, y;
char ignore;
cin >> x >> ignore >> y;
Point p1(x, y);
cin >> x >> ignore >> y;
Point p2(x, y);
cout << "(" << p1.getX() << "," << p1.getY() << ")" << endl;
cout << "(" << p2.getX() << "," << p2.getY() << ")" << endl;
return 0;
}
【描述】
声明并实现一个Rectangle类,表示矩形。Rectangle类包括:
double类型的私有数据成员width和height,表示矩形的宽和高。
带默认参数的构造函数,将矩形的宽和高设置为给定的参数。宽和高的默认参数值为1。
更改器函数setWidth和setHeight,分别用于修改矩形的宽和高。
访问器函数getWidth和getHeight,分别用于访问矩形的宽和高。
成员函数computeArea,返回矩形的面积。
成员函数computePerimeter,返回矩形的周长。
【输入】
5 40
10 3.5
【输出】
200 90
35 27
【来源】
《程序设计基础——以C++为例》第5章实验2。
#include <iostream>
using namespace std;
/* 请在此处编写Rectangle类 */
class Rectangle
{
private:
double width;//矩形的宽和高
double height;
public:
Rectangle(double width=1,double height=1)//宽和高的默认参数值为1
{
this->width=width;
this->height=height;
}
void setWidth(double width)//修改矩形的宽和高
{
this->width=width;
}
void setHeight(double height)
{
this->height=height;
}
double getWidth()//访问矩形的宽和高
{
return this->width;
}
double getHeight()
{
return this->height;
}
double computeArea()//返回矩形的面积
{
return this->width*this->height;
}
double computePerimeter()//返回矩形的周长
{
return this->width*2+this->height*2;
}
};
int main() {
double width, height;
cin >> width >> height;
Rectangle rect1;
rect1.setWidth(width);
rect1.setHeight(height);
cin >> width >> height;
Rectangle rect2(width, height);
cout << rect1.computeArea() << " " << rect1.computePerimeter() << endl;
cout << rect2.computeArea() << " " << rect2.computePerimeter() << endl;
return 0;
}
【描述】
声明并实现一个Cylinder类,表示圆柱体。Cylinder类包括:
double类型的私有数据成员radius和height,分别表示圆柱体底半径和高。
带默认参数的构造函数,将圆柱体底半径和高设置为给定的参数。半径和高的默认参数值为1。
访问器函数,分别用于访问圆柱体底半径和高。
成员函数computeVolume,返回圆柱体体积。
成员函数computeSurfaceArea,返回圆柱体表面积。
假设PI为3.14159。
【输入】
输入圆柱体的底半径和高。
【输出】
输出圆柱体的体积和表面积。
【输入示例】
4 8
【输出示例】
402.124
301.593
【来源】
《程序设计基础——以C++为例》第5章实验3。
#include <iostream>
using namespace std;
const double PI = 3.14159;
/* 请在此处编写Cylinder类 */
class Cylinder
{
private: //私有数据成员radius和height
double radius;
double height;
public:
Cylinder(double radius=1,double height=1)//带默认参数的构造函数
{
this->radius=radius;
this->height=height;
}
double computeVolume()//返回圆柱体体积
{
return PI*this->radius*this->radius*this->height;
}
double computeSurfaceArea()//返回圆柱体表面积
{
return PI*this->radius*2*this->height+2*PI*this->radius*this->radius;
}
//访问器函数,分别用于访问圆柱体底半径和高,题目要求做,但程序用不到,象征性写一下
double getRadius()
{
return this->radius;
}
double getHeight()
{
return this->height;
}
};
int main() {
double radius, height;
cin >> radius >> height;
Cylinder cylinder(radius, height);
cout << cylinder.computeVolume() << endl;
cout << cylinder.computeSurfaceArea() << endl;
return 0;
}
【描述】
声明并实现一个Account类,表示银行账户。Account类包括:
string类型的私有数据成员id,表示账号;string类型的私有数据成员name,表示客户名;double类型的私有数据成员balance,表示账户余额;double类型的私有数据成员annualInterestRate,表示年利率。
有参构造函数,将账号、客户名、账户余额、年利率设置为给定的参数。
更改器函数setId、setName、setBalance和setAnnualInterestRate,分别用于修改账号、客户名、账户余额、年利率。
访问器函数getId、getName、getBalance和getAnnualInterestRate,分别用于访问账号、客户名、账户余额、年利率。
成员函数withdraw,从账户中取款。
成员函数deposit,往账户中存款。
成员函数computeMonthlyInterestRate,返回月利率。
成员函数print,输出账户信息。
【输入】
输入账号、客户名、账户余额和年利率。
【输出】
输出账号、客户名、账户余额和月利率。
【输入示例】
112233 ZhangSan 20000 4.5
【输出示例】
112233
ZhangSan
20500
0.375%
【提示】
假设年利率为4.5%,则以4.5作为输入值。
月利率=年利率/12
【来源】
《程序设计基础——以C++为例》第5章实验4。
#include <iostream>
#include <string>
using namespace std;
/* 请在此处编写Account类 */
class Account
{
private:
string id;//账号
string name;//客户名
double balance;//账户余额
double annualInterestRate;//年利率
public:
Account(string id,string name,double balance,double annualInterestRate)//构造函数
{
this->id=id;
this->name=name;
this->balance=balance;
this->annualInterestRate=annualInterestRate;
}
//访问,修改账号、客户名、账户余额、年利率
void setId(double id)
{
this->id=id;
}
string getId()
{
return this->id;
}
void setName(double name)
{
this->name=name;
}
string getName()
{
return this->name;
}
void setBalance(double balance)
{
this->balance=balance;
}
double getBalance()
{
return this->balance;
}
void setAnnualInterestRate(double annualInterestRate)
{
this->annualInterestRate=annualInterestRate;
}
double getAnnualInterestRate()
{
return this->annualInterestRate;
}
void withdraw(double n)//从账户中取款
{
this->balance-=n;
}
void deposit(double n)//从账户中存款
{
this->balance+=n;
}
double computeMonthlyInterestRate(double annualInterestRate)//返回月利率
{
return annualInterestRate/12;//月利率
}
void print()//输出账户信息
{
cout<<this->id<<endl<<this->name<<endl<<this->balance<<endl<<this->annualInterestRate/12/100<<"%";
}
};
int main() {
string id;
string name;
double balance;
double annualInterestRate;
cin >> id >> name >> balance >> annualInterestRate;
Account account(id, name, balance, annualInterestRate);
account.withdraw(2500);
account.deposit(3000);
account.print();
return 0;
}
写c艹比较顺了, 因为会一丢丢java, 如有错误, 还望指出, 非常感谢, 欢迎讨论题目