C++面向对象的程序设计谭浩强 第四章课后题

  以往章节

C++面向对象的程序设计谭浩强 第二章课后题

C++面向对象的程序设计谭浩强 第三章课后题

C++面向对象的程序设计谭浩强 第四章课后题

C++面向对象的程序设计谭浩强 第五章课后题

C++面向对象的程序设计谭浩强 第六章课后题

C++面向对象的程序设计谭浩强 第七章课后题

C++面向对象的程序设计谭浩强 第八章课后题

1. (简答题) 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。

#include <iostream>
using namespace std;
class Complex
{
public:
	int real;
	int imag;
	//成员函数运算符重载
	/*Complex operator +(Complex& c2);*/
	void get_complex();
	void dispaly();
};
//类外实现
//Complex Complex::operator+(Complex& c2)
//{
//	Complex c1;
//	c1.real = real + c2.real;
//	c1.imag = imag + c2.imag;
//	return c1;
//}
void Complex::get_complex()
{
	cin >> real >> imag;
}
void Complex::dispaly()
{
	cout << "(" << real << "," << imag << "i)" << endl;
}
//全局函数来进行运算符重载
Complex operator+(Complex &c1,Complex &c2) {
	Complex temp;
	temp.real = c1.real + c2.real;
	temp.imag = c2.imag + c2.imag;
	return temp;
}
int main()
{
	Complex c1, c2, c3;
	c1.get_complex();
	c2.get_complex();

	c3 = operator+(c1, c2);
	//c3 = c1.operator+(c2);
	c3.dispaly();
	c3 = c1 + c2;
	c3.dispaly();
}

2. (简答题)定义一个复数类Complex,重载运算符“+”,“-”,“*”,“/”,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数之和、差、积和商。

#include <iostream>
using namespace std;
class Complex
{
public:
	int real;
	int imag;
	Complex operator +(Complex& c2);
	Complex operator-(Complex& c2);
	Complex operator*(Complex& c2);
	Complex operator/(Complex& c2);

	void get_complex();
	void dispaly();
};
Complex Complex::operator+(Complex& c2)
{
	Complex c1;
	c1.real = real + c2.real;
	c1.imag = imag + c2.imag;
	return c1;
}
Complex Complex::operator-(Complex& c2)
{
	Complex c1;
	c1.real = real - c2.real;
	c1.imag = imag - c2.imag;
	return c1;
}
Complex Complex::operator*(Complex& c2)
{
	Complex c1;
	c1.real = real * c2.real;
	c1.imag = imag * c2.imag;
	return c1;
}
Complex Complex::operator/(Complex& c2)
{
	Complex c1;
	int a = c2.real * c2.real + c2.imag * c2.imag;
	c1.real = (real * c2.real + imag * c2.imag) / a;
	c1.imag = (imag * c2.real - real * c2.imag) / a;
	return c1;
}
void Complex::get_complex()
{
	cin >> real >> imag;
}
void Complex::dispaly()
{
	cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
	Complex c1, c2, c3;
	c1.get_complex();
	c2.get_complex();
	c3 = c1 + c2;
	c3.dispaly();
	c1.get_complex();
	c2.get_complex();
	c3 = c1 - c2;
	c3.dispaly();
	c1.get_complex();
	c2.get_complex();
	c3 = c1 * c2;
	c3.dispaly();
	c1.get_complex();
	c2.get_complex();
	c3 = c1 / c2;
	c3.dispaly();
}

3. (简答题)定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如: c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。编程序,分别求两个复数之和、整数和复数之和。

#include <iostream>
using namespace std;
class Complex
{
public:
	Complex operator +(Complex& c2);
    //运算符的重载也可以进行函数重载
	Complex operator +(int a);
	void get_complex();
	void dispaly();
private:
	int real;
	int imag;
};
Complex Complex::operator+(Complex& c2)
{
	Complex c1;
	c1.real = real + c2.real;
	c1.imag = imag + c2.imag;
	return c1;
}
Complex Complex::operator+(int a)
{
	Complex c1;
	c1.real = real + a;
	c1.imag = imag;
	return c1;
}
void Complex::get_complex()
{
	cin >> real >> imag;
}
void Complex::dispaly()
{
	cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
	Complex c1, c2,c3;
	int a = 0;
	c1.get_complex();
	c2.get_complex();
	c3 = c1 + c2;
	c3.dispaly();
	c2.get_complex();
	cin >> a;
	c3 = c2 + a;
	c3.dispaly();
}

4. (简答题)有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如: c=a+b。

#include <iostream>
using namespace std;
class Array
{
public:
    friend Array operator+(Array a1, Array a2);
    void set();
    void display();
private:
    int arr[2][3];
};
Array operator+(Array a1, Array a2)
{
    Array a3;
    int i, j;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
            a3.arr[i][j] = a2.arr[i][j] + a1.arr[i][j];
        }
    }
    return a3;
}
void Array::set()
{
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            cin >> arr[i][j];
        }
    }
}
void Array::display()
{
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            cout << arr[i][j] << " ";
        }
    }
}
int main()
{
    Array a1, a2, a3;
    a1.set();
    a2.set();
    a3 = a1 + a2;
    a3.display();
    return 0;
}

5. (简答题)在第4题的基础上,重载流插入运算符“<<”和流提取运算符“>>”,使之能用于该矩阵的输入和输出。

#include <iostream>
using namespace std;
class Array
{
public:
    friend Array operator+(Array a1, Array a2);
    friend istream& operator>>(istream&, Array&);
    friend ostream& operator<<(ostream&, Array&);
private:
    int arr[2][3];
};
Array operator+(Array a1, Array a2)
{
    Array a3;
    int i, j;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
            a3.arr[i][j] = a2.arr[i][j] + a1.arr[i][j];
        }
    }
    return a3;
}
istream& operator>>(istream&, Array&)
{
    Array a3;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            cout << a3.arr[i][j] << " ";
        }
    }
}
ostream& operator<<(ostream&, Array&)
{
    Array a3;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            cin >> a3.arr[i][j];
        }
    }
}
int main()
{
    Array a1, a2, a3;
    cin >> a1;
    cin >> a2;
    a3 = a1 + a2;
    cout << a3;
    return 0;
}

6. (简答题)请编写程序,处理一个复数与一个double数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义Complex(复数)类,在成员函数中包含重载类型转换运算符: operator double( ){return real;}

#include <iostream>
using namespace std;
class Complex
{
public:
    Complex(double r) { real = r; imag = 0; }
    Complex(double r, double i) { real = r; imag = i; }
    operator double() { return real; }
    void display();
private:
    double real;
    double imag;
};
void Complex::display()
{
    cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
    Complex c(1, -1);
    double d1;
    d1 = c + 6.6;
    cout << d1 << endl;
    Complex(d1).display();
    return 0;
}

7. (简答题)定义一个Teacher(教师)类和一个Student(学生)类,二者有一部分数据成员是相同的,例如num(号码),name(姓名),sex(性别)。编写程序,将一个Student对象(学生)转换为Teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为: 一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。

#include <iostream>
#include <string>
using namespace std;
class student 
{
public:
    student(int, char[], char);
    int get_num() { return num; }
    char* get_name() { return name; }
    char get_sex() { return sex; }
private:
    int num;
    char name[20];
    char sex;
};
student::student(int n, char nam[], char s)
{
    num = n;
    strcpy_s(name, nam);
    sex = s;
}
class teacher 
{
public:
    teacher(int, char[], char, int);
    teacher(student&);
    void display();
private:
    int num;
    char name[20];
    char sex;
    int pay;
};
teacher::teacher(int n, char nam[], char s, int p)
{
    num = n;
    strcpy_s(name, nam);
    sex = s;
    pay = p;
}
teacher::teacher(student& s)
{
    num = s.get_num();
    strcpy(name, s.get_name());
    sex = s.get_sex();
    pay = 6666;
}
void teacher::display()
{
    cout << num;
    cout << name;
    cout << sex;
    cout << pay;
}
int main()
{
    student s1(3, "席xx", 'm');
    teacher t1(2, "席teacher", 'w', 9999);
    teacher t2 = teacher(s1);
    t1.display();
    t2.display();
    return 0;
}

  • 13
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五毛变向.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值