题目:
1、声明一个复数类Complex,重载运算符“+”,“-”,“*”,“/”,使之能用于复数的加、减、乘、除,运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数之和、差、积和商。
思路:复数分为实部和虚部,定义包含实部和虚部的复数类,将运算符重载为分别计算实部和虚部的值再输出。
2、声明一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参数运算的两个运算量可以都是类的对象,也可以其中有一个是整数,顺序任意。例如,C1+C2,i+C1,C1+i均合法(设i为整数,c1,c2 为复数)。
3、有两个矩阵a和b,均为2 行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如c=a+b。
思路:使用二维数组存储矩阵,重载运算符实现两二维数组对应元素相加并输出。
4、声明一个Teacher(教师)类和一个student (学生)类,二者有一部分数据成员是相同的,例如num(号码),name(姓名),sex(性别)。编写程序,将一个student 对象(学生)转换为teacher(教师)类,只将以上3个相加的数据成员移植过去。
参考代码:
1、
#include<iostream>
using namespace std;
class Complex{
public:
Complex(){
real = 0;
imag = 0;
}
Complex(double r,double i){
real = r;
imag = i;
}
Complex operator +(Complex &c2);
Complex operator -(Complex &c2);
Complex operator *(Complex &c2);
Complex operator /(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator +(Complex &c2){
Complex c;
c.real = real+c2.real;
c.imag = imag+c2.imag;
return c;
}
Complex Complex::operator -(Complex &c2){
Complex c;
c.real = real-c2.real;
c.imag = imag-c2.imag;
return c;
}
Complex Complex::operator *(Complex &c2){
Complex c;
c.real = real*c2.real;
c.imag = imag*c2.imag;
return c;
}
Complex Complex::operator /(Complex &c2){
Complex c;
c.real = real/c2.real;
c.imag = imag/c2.imag;
return c;
}
void Complex::display(){
cout<<"("<<real<<","<<imag<<"i"<<")"<<endl;
}
int main(){
Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
c3.display();
c3=c1-c2;
c3.display();
c3=c1*c2;
c3.display();
c3=c1/c2;
c3.display();
}
2、
#include <iostream.h>
class Complex
{
public:
Complex() { real = 0; imag = 0; }
Complex(double r, double i) { real = r; imag = i; }
Complex operator+(Complex& c2);
Complex operator+(int& i);
friend Complex operator+(int&, Complex&);
void display();
private:
double real;
double imag;
};
Complex Complex::operator+(Complex& c)
{
return Complex(real + c.real, imag + c.imag);
}
Complex Complex::operator+(int& i)
{
return Complex(real + i, imag);
}
void Complex::display()
{
cout << "(" << real << "," << imag << "i)" << endl;
}
Complex operator+(int& i, Complex& c)
{
return Complex(i + c.real, c.imag);
}
int main()
{
Complex c1(3, 4), c2(5, -10), c3;
int i = 5;
c3 = c1 + c2;
cout << "c1+c2=";
c3.display();
c3 = i + c1;
cout << "i+c1=";
c3.display();
c3 = c1 + i;
cout << "c1+i=";
c3.display();
return 0;
}
3、
#include <iostream.h>
class juzhen
{
public:
juzhen();
friend juzhen operator+(juzhen&, juzhen&);
void input();
void display();
private:
int mat[2][3];
};
juzhen::juzhen()
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
mat[i][j] = 0;
}
juzhen operator+(juzhen& a, juzhen& b)
{
juzhen c;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
c.mat[i][j] = a.mat[i][j] + b.mat[i][j];
}
return c;
}
void juzhen::input()
{
cout << "输入矩阵元素" << endl;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
cin >> mat[i][j];
}
void juzhen::display()
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}
int main()
{
juzhen a, b, c;
a.input();
cout << endl << " a:" << endl;
a.display();
b.input();
cout << endl << " b:" << endl;
b.display();
c = a + b;
cout << endl << " c = a + b :" << endl;
c.display();
return 0;
}
4、
#include<iostream>
#include<cstring>
using namespace std;
class Student
{
public:
Student(int n, const char nam[], char s, float sco)
{
num = n;
strcpy(name, nam);
sex = s;
score = sco;
}
int get_num() {return num;}
char* get_name() {return name;}
char get_sex() {return sex;}
void display()
{
cout << "num:" << num << "\nname:" << name << "\nsex:" << sex << "\nscore:" << score << "\n\n";
}
private:
int num;
char name[20];
char sex;
float score;
};
class Teacher
{
public:
Teacher(){}
Teacher(int n, const char nam[], char s, float p)
{
num = n;
strcpy(name, nam);
sex = s;
pay = p;
}
Teacher(Student&);
void display()
{
cout << "num:" << num << "\nname:" << name << "\nsex:" << sex << "\npay:" << pay << "\n\n";
}
private:
int num;
char name[20];
char sex;
float pay;//工资
};
Teacher::Teacher(Student& stud)
{
num = stud.get_num();
strcpy(name, stud.get_name());
sex = stud.get_sex();
pay = 1500;
}
int main()
{
Student student1(20010, "Wang", 'm', 89.5);
Teacher teacher1(10001, "Li", 'f', 1234.5), teacher2;
cout << "student1:" << endl;
student1.display();
cout << "teacher1:" << endl;
teacher1.display();
teacher2 = Teacher(student1);
cout << "teacher2:" << endl;
teacher2.display();
return 0;
}