1. 定义一个点类, 其数据成员包括点的坐标,并提供计算两点之间距离的成员函数 2. 定义一个矩形类, 其数据成员包括长度和宽度,并提供计算矩形面积的成员函数 3. 定义一个三角形类,其数据成员包括边长,并提供计算三角形面积的成员函数 4. 定义一个圆形类, 其数据成员包括圆心坐标和半径,并实例化两个圆形对象 5. 定义一个日期类, 其数据成员包括年、月、日,并提供求闰年、星期的成员函数 6. 定义一个时间类, 其数据成员包括时、分、秒,并提供求总秒数的成员函数 7. 定义一个储户类, 其数据成员包括储户姓名、账号、存款,并提供存入指定的存款、取出指定的存款的成员函数 8. 定义一个股票类, 其数据成员包括公司名称、持有股票数量、每股价格、总价格,并提供增加持有股票、减少持有股票成员函数 9. 定义一个书类, 其数据成员包括书名、价格、出版日期,并提供出版年限的成员函数,需要重用前面习题的日期类 10.定义一个学生类, 其数据成员包括姓名、年龄,出生年月,提供计算年龄的成员函数,需要重用前面习题的日期类
时间: 2023-06-07 17:06:55 浏览: 131
java计算雷达屏幕上两点之间距离的应用
3星 · 编辑精心推荐
1. 定义一个点类:
class Point {
private:
int x, y;
public:
Point(int x, int y) {
this->x = x;
this->y = y;
}
double distance(Point p) {
return sqrt(pow(x - p.x, 2) + pow(y - p.y, 2));
}
};
2. 定义一个矩形类:
class Rectangle {
private:
int length, width;
public:
Rectangle(int length, int width) {
this->length = length;
this->width = width;
}
int area() {
return length * width;
}
};
3. 定义一个三角形类:
class Triangle {
private:
double a, b, c;
public:
Triangle(double a, double b, double c) {
this->a = a;
this->b = b;
this->c = c;
}
double area() {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
};
4. 定义一个圆形类:
class Circle {
private:
Point center;
double radius;
public:
Circle(Point center, double radius) {
this->center = center;
this->radius = radius;
}
Point getCenter() {
return center;
}
double getRadius() {
return radius;
}
};
Circle c1(Point(0, 0), 1);
Circle c2(Point(3, 4), 2);
5. 定义一个日期类:
class Date {
private:
int year, month, day;
public:
Date(int year, int month, int day) {
this->year = year;
this->month = month;
this->day = day;
}
bool isLeapYear() {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
}
else {
return false;
}
}
int getWeekday() {
int y = year, m = month, d = day;
if (m == 1 || m == 2) {
m += 12;
y--;
}
int w = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
return w;
}
};
6. 定义一个时间类:
class Time {
private:
int hour, minute, second;
public:
Time(int hour, int minute, int second) {
this->hour = hour;
this->minute = minute;
this->second = second;
}
int getTotalSeconds() {
return hour * 3600 + minute * 60 + second;
}
};
7. 定义一个储户类:
class Depositor {
private:
string name, account;
double balance;
public:
Depositor(string name, string account, double balance) {
this->name = name;
this->account = account;
this->balance = balance;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
balance -= amount;
}
};
8. 定义一个股票类:
class Stock {
private:
string company;
int quantity;
double price, value;
public:
Stock(string company, int quantity, double price) {
this->company = company;
this->quantity = quantity;
this->price = price;
value = quantity * price;
}
void buy(int quantity, double price) {
this->quantity += quantity;
this->price = price;
value = this->quantity * price;
}
void sell(int quantity, double price) {
this->quantity -= quantity;
this->price = price;
value = this->quantity * price;
}
};
9. 定义一个书类:
class Book {
private:
string title;
double price;
Date publishDate;
public:
Book(string title, double price, Date publishDate) {
this->title = title;
this->price = price;
this->publishDate = publishDate;
}
int getAge() {
Date today(2021, 11, 5);
int age = today.getYear() - publishDate.getYear();
if (today.getMonth() < publishDate.getMonth() ||
(today.getMonth() == publishDate.getMonth() && today.getDay() < publishDate.getDay())) {
age--;
}
return age;
}
};
10. 定义一个学生类:
class Student {
private:
string name;
int age;
Date birthDate;
public:
Student(string name, int age, Date birthDate) {
this->name = name;
this->age = age;
this->birthDate = birthDate;
}
int getAge() {
Date today(2021, 11, 5);
int age = today.getYear() - birthDate.getYear();
if (today.getMonth() < birthDate.getMonth() ||
(today.getMonth() == birthDate.getMonth() && today.getDay() < birthDate.getDay())) {
age--;
}
return age;
}
};
阅读全文