1.设计一个程序,将加法运算符和减法运算符分别重载为成员函数,使之能够实现复数的加法运算与减法运算。将乘法运算符和除法运算符分别重载为友元函数,使之能够实现复数的乘法运算与除法运算。 2. 定义一个学生(Student)类,包括学号(整型)、性别(字符型)、姓名(字符串类型)、成绩(浮点型)四个数据成员,编程实现对流插入运算符(<<)和流提取符(>>)的重载,使它们能够对Student类对象直接进行输入和输出。 3.声明抽象基类Shape,由它派生出5个图形类;Circle(圆形)、Square(正方形)、Rectangle(矩形)、Trapezoid(梯形)、Triangle(三角形),用虚函数分别计算几种图形的面积,并求它们的和。要求用基类指针数组,使它的每一个元素指向一个派生类对象。
时间: 2023-06-16 22:08:19 浏览: 171
1. 复数类的加减乘除运算符重载代码如下:
```c++
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex operator+(const Complex& c) const;
Complex operator-(const Complex& c) const;
friend Complex operator*(const Complex& c1, const Complex& c2);
friend Complex operator/(const Complex& c1, const Complex& c2);
friend ostream& operator<<(ostream& os, const Complex& c);
};
Complex Complex::operator+(const Complex& c) const {
return Complex(real + c.real, imag + c.imag);
}
Complex Complex::operator-(const Complex& c) const {
return Complex(real - c.real, imag - c.imag);
}
Complex operator*(const Complex& c1, const Complex& c2) {
return Complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real);
}
Complex operator/(const Complex& c1, const Complex& c2) {
double denominator = c2.real * c2.real + c2.imag * c2.imag;
return Complex((c1.real * c2.real + c1.imag * c2.imag) / denominator, (c1.imag * c2.real - c1.real * c2.imag) / denominator);
}
ostream& operator<<(ostream& os, const Complex& c) {
os << "(" << c.real << ", " << c.imag << "i)";
return os;
}
int main() {
Complex c1(1, 2), c2(3, 4);
cout << c1 + c2 << endl; // (4, 6i)
cout << c1 - c2 << endl; // (-2, -2i)
cout << c1 * c2 << endl; // (-5, 10i)
cout << c1 / c2 << endl; // (0.44, -0.08i)
return 0;
}
```
2. 学生类的流插入运算符和流提取符重载代码如下:
```c++
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int id;
char gender;
string name;
double score;
public:
Student(int i = 0, char g = 'F', string n = "", double s = 0) : id(i), gender(g), name(n), score(s) {}
friend ostream& operator<<(ostream& os, const Student& s);
friend istream& operator>>(istream& is, Student& s);
};
ostream& operator<<(ostream& os, const Student& s) {
os << s.id << " " << s.gender << " " << s.name << " " << s.score;
return os;
}
istream& operator>>(istream& is, Student& s) {
is >> s.id >> s.gender >> s.name >> s.score;
return is;
}
int main() {
Student s(123, 'F', "Alice", 89.5);
cout << s << endl; // 123 F Alice 89.5
Student t;
cin >> t;
cout << t << endl;
return 0;
}
```
3. 图形类的面积计算和基类指针数组代码如下:
```c++
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r = 0) : radius(r) {}
virtual double area() const {
return 3.14159 * radius * radius;
}
};
class Square : public Shape {
private:
double side;
public:
Square(double s = 0) : side(s) {}
virtual double area() const {
return side * side;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l = 0, double w = 0) : length(l), width(w) {}
virtual double area() const {
return length * width;
}
};
class Trapezoid : public Shape {
private:
double top;
double bottom;
double height;
public:
Trapezoid(double t = 0, double b = 0, double h = 0) : top(t), bottom(b), height(h) {}
virtual double area() const {
return (top + bottom) * height / 2;
}
};
class Triangle : public Shape {
private:
double base;
double height;
public:
Triangle(double b = 0, double h = 0) : base(b), height(h) {}
virtual double area() const {
return base * height / 2;
}
};
int main() {
Shape* shapes[5];
shapes[0] = new Circle(2);
shapes[1] = new Square(3);
shapes[2] = new Rectangle(4, 5);
shapes[3] = new Trapezoid(2, 4, 3);
shapes[4] = new Triangle(2, 3);
double totalArea = 0;
for (int i = 0; i < 5; i++) {
totalArea += shapes[i]->area();
delete shapes[i];
}
cout << "Total area: " << totalArea << endl;
return 0;
}
```
阅读全文