赞
踩
练习题1
解题思路:题目中描述是非成员,非友元的普通函数,又要实现复数的运算。第一感觉应该把real和imag设置成public,但是这样就破坏了C++的封装性,所以,我设置了一个getReal()和getImag()的函数,获取real和imag数据
- //类声明文件
- #include<iostream>
- using namespace std;
- class Complex
- {
- public:
- //构造函数
- Complex();
- //构造构造函数
- Complex(int ,int );
- int getReal();
- int getImag();
- void display();
- private:
- int real;
- int imag;
- };
-
- //类实现文件
- #include<iostream>
- #include "complex.h"
- using namespace std;
- Complex::Complex():real(0),imag(0){}
- Complex::Complex(int r,int i):real(r),imag(i){}
- int Complex::getReal()
- {
- return this->real;
- }
- int Complex::getImag()
- {
- return this->imag;
- }
- void Complex::display()
- {
- cout<<this->real<<" + "<<this->imag<<"i"<<endl;
- }
-
- //主函数文件
- #include<iostream>
- #include "complex.h"
- using namespace std;
- Complex operator +( Complex &complex1, Complex &complex2)
- {
- return (Complex(complex1.getReal()+complex2.getReal(),complex1.getImag()+complex2.getImag()));
-
- }
- int main()
- {
- class Complex complex1(3,30),complex2(5,20);
- Complex complex3 = complex1 + complex2;
- complex3.display();
-
- system("pause");
- return 0;
- }
练习题2
- //类声明文件
- #include<iostream>
- using namespace std;
- class Complex
- {
- public:
- //构造函数
- Complex();
- //构造构造函数
- Complex(int ,int );
- Complex operator +( Complex &complex);
- Complex operator -( Complex &complex);
- Complex operator *( Complex &complex);
- Complex operator /( Complex &complex);
- int getReal();
- int getImag();
- void display();
- private:
- int real;
- int imag;
- };
-
- //类实现文件
- #include<iostream>
- #include "complex.h"
- using namespace std;
- Complex::Complex():real(0),imag(0){}
- Complex::Complex(int r,int i):real(r),imag(i){}
- Complex Complex::operator +( Complex &complex)
- {
- return (Complex(this->real + complex.real,this->imag + complex.imag));
-
- }
- Complex Complex::operator -( Complex &complex)
- {
- return (Complex(this->real - complex.real,this->imag - complex.imag));
-
- }
- Complex Complex::operator *( Complex &complex)
- {
- return (Complex(this->real * complex.real - this->imag * complex.imag,this->real * complex.imag + this->imag * complex.real));
-
- }
- Complex Complex::operator /( Complex &complex)
- {
- int Denominator = complex.real * complex.real + complex.imag * complex.imag;
- if(Denominator != 0)
- {
-
- return (Complex((this->real * complex.real + this->imag * complex.imag)/Denominator,(this->imag * complex.real - this->real * complex.imag)/Denominator));
-
- }
-
- }
-
- int Complex::getReal()
- {
- return this->real;
- }
- int Complex::getImag()
- {
- return this->imag;
- }
- void Complex::display()
- {
- cout<<this->real<<" + "<<this->imag<<"i"<<endl;
- }
-
- //主函数文件
- #include<iostream>
- #include "complex.h"
- using namespace std;
-
- int main()
- {
- class Complex complex1(3,30),complex2(5,20);
- Complex complex3 = complex1 + complex2;
-
- complex3.display();
- Complex complex4 = complex1 - complex2;
- complex4.display();
-
- Complex complex5 = complex1 * complex2;
- complex5.display();
-
- Complex complex6 = complex1 / complex2;
- complex6.display();
-
-
- system("pause");
- return 0;
- }
练习题3
解题思路:根据题目描述,可以决定采用友元函数+转换构造函数可以实现加法交换律
- //类声明文件
- #include<iostream>
- using namespace std;
- class Complex
- {
- public:
- //构造函数
- Complex();
- //构造构造函数
- Complex(int ,int );
- //转换构造函数
- Complex(int );
- friend Complex operator +( const Complex &,const Complex &);
- int getReal();
- int getImag();
- void display();
- private:
- int real;
- int imag;
- };
-
- //类实现文件
- #include<iostream>
- #include "complex.h"
- using namespace std;
- Complex::Complex():real(0),imag(0){}
- Complex::Complex(int r):real(r),imag(0){}// 转换构造函数
- Complex::Complex(int r,int i):real(r),imag(i){}
- Complex operator +( const Complex &complex1,const Complex &complex2)
- {
- return (Complex(complex1.real + complex2.real,complex1.imag + complex2.imag));
-
- }
- int Complex::getReal()
- {
- return this->real;
- }
- int Complex::getImag()
- {
- return this->imag;
- }
- void Complex::display()
- {
- cout<<this->real<<" + "<<this->imag<<"i"<<endl;
- }
-
- //主函数文件
- #include<iostream>
- #include "complex.h"
- using namespace std;
-
- int main()
- {
- class Complex complex1(3,30),complex2(5,20);
- Complex complex3 = complex1 + complex2;
- complex3.display();
- complex3 = 2 + complex2;
- complex3.display();
- complex3 = complex2 + 2;
- complex3.display();
- system("pause");
- return 0;
- }
-
练习题4+5
解题思路:在矩阵中我用了二维数组指针,同时还用了指针的指针。在析构函数中释放指针的指针,在浅拷贝中容易造成野指针,所以我使用了复制构造函数,解决上述问题。
指针的指针接收指针数组的过程,参考博客:https://blog.csdn.net/ChaoFeiLi/article/details/103674308
- //类声明文件
- #include<iostream>
- using namespace std;
- class Matrix
- {
- public:
- Matrix(int = 1,int = 1);
- Matrix(const Matrix&);
- friend Matrix operator +(const Matrix&,const Matrix&);
- friend ostream& operator <<(ostream&,const Matrix&);
- friend istream& operator >>(istream&,const Matrix&);
- void set();
- void set(double **,int,int);
- void display();
- ~Matrix(){
- delete []ptr;
- }
- protected:
- float **ptr;
- private:
-
- int row;
- int column;
- };
-
- //类实现文件
- #include<iostream>
- #include "matrix.h"
- using namespace std;
- Matrix::Matrix(int r ,int c)
- {
- if(r ==0 || c == 0)
- {
- return;
- }
- row = r;
- column = c;
- ptr = new float* [row];
- for(int i = 0; i < row; i++)
- {
- ptr[i] = new float [column];
- }
- }
- Matrix::Matrix(const Matrix&matrix)
- {
- this->row = matrix.row;
- this->column = matrix.column;
- this->ptr = new float*[row];
- for(int i = 0;i<row;i++)
- {
- ptr[i] = new float[column];
- for(int j = 0;j<column;j++)
- {
- this->ptr[i][j] = matrix.ptr[i][j];
- }
- }
- }
- Matrix operator +(const Matrix &matrix1,const Matrix &matrix2)
- {
- int row = matrix1.row;
- int column = matrix1.column;
- Matrix matrix(row,column);
-
- for(int i = 0;i < row;i++)
- {
- for(int j = 0;j < column;j++)
- {
- matrix.ptr[i][j] = matrix1.ptr[i][j] + matrix2.ptr[i][j];
- }
- }
- return matrix;
- }
-
-
- ostream& operator <<(ostream &output,const Matrix &matrix)
- {
- int row = matrix.row;
- int column = matrix.column;
- for(int i = 0;i < row;i++)
- {
- for(int j = 0;j < column;j++)
- {
- output<<matrix.ptr[i][j]<<" ";
-
- }
- output<<endl;
-
- }
-
-
- return output;
-
- }
- istream& operator >>(istream& input,const Matrix&matrix)
- {
- int row = matrix.row;
- int column = matrix.column;
- for(int i = 0;i < row;i++)
- {
- for(int j = 0;j < column;j++)
- {
- input>>matrix.ptr[i][j];
-
- }
-
- }
- return input;
-
- }
- void Matrix::set()
- {
- for(int i = 0;i < row;i++)
- {
- for(int j = 0;j < column;j++)
- {
- cin>>ptr[i][j];
-
- }
-
- }
-
- }
- void Matrix::set(double **p,int r,int c)
- {
- ptr = new float *[row];
- for (int i = 0;i < row;i++)
- {
- ptr[i] = new float[column];
- for (int j = 0;i<column;j++)
- {
- ptr[i][j] = p[i][j];
- }
- }
-
-
- }
- void Matrix::display()
- {
- for(int i = 0;i < row;i++)
- {
- for(int j = 0;j < column;j++)
- {
- cout<<ptr[i][j]<<" ";
-
- }
- cout<<endl;
-
- }
- }
-
- //主函数文件
- #include<iostream>
- #include "matrix.h"
- using namespace std;
- int main()
- {
-
-
- Matrix matrix1(2,3);
- cin>>matrix1;
- cout<<endl;
- Matrix matrix2(2,3);
- cin>>matrix2;
- Matrix matrix = matrix1 + matrix2;
- cout<<matrix;
-
- system("pause");
- return 0;
- }
练习题6
- //类声明文件
- #include<iostream>
- using namespace std;
- class Complex
- {
- public:
- //构造函数
- Complex();
- //构造构造函数
- Complex(double,double);
- //转换构造函数
- Complex(double);
- //类型转换函数
- operator double() const;
- void display();
- private:
- double real;
- double imag;
- };
- //类实现文件
- #include<iostream>
- #include "complex.h"
- using namespace std;
- Complex::Complex():real(0),imag(0){}
- Complex::Complex(double r):real(r),imag(0){}
- Complex::Complex(double r,double i):real(r),imag(i){}
-
- void Complex::display()
- {
- cout<<this->real<<" + "<<this->imag<<"i"<<endl;
- }
- Complex::operator double() const
- {
- return this->real;
- }
-
- //主函数文件
- #include<iostream>
- #include "complex.h"
- using namespace std;
- int main()
- {
- class Complex complex1(3,30);
- double d1 = 2.5;
- d1 = d1 + complex1;
- cout<<d1<<endl;
- Complex(d1).display();
- system("pause");
- return 0;
- }
练习题7
必须把class Student中的成员变量设置为public的,或者通过get函数获取;在转换构造函数中无法使用
- //类声明文件
- #include <iostream>
- class Student
- {
- public:
- Student(int,char [],char);
- void display();
- int num;
- char name[50];
- char sex;
-
- };
- class Teacher
- {
- public:
- Teacher(int,char [],char);
- //转换构造函数
- Teacher(const Student&);
- void display();
- private:
- int num;
- char name[50];
- char sex;
-
- };
- //类实现文件
- #include <iostream>
- #include "worker.h"
- using namespace std;
- Student::Student(int n,char na[],char s)
- {
- this->num = n;
- strcpy(this->name, na);
- this->sex = s;
- }
- void Student::display()
- {
- cout <<name<<endl;
- }
- Teacher::Teacher(int n,char na[],char s)
- {
- this->num = n;
- strcpy(this->name, na);
- this->sex = s;
- }
- Teacher::Teacher(const Student &student)
- {
- this->num = student.num;
- strcpy(this->name, student.name);
- this->sex = student.sex;
- }
- void Teacher::display()
- {
- cout<<this->name<<" "<<this->num<<" "<<this->sex<<endl;
- }
- //主函数文件
- #include<iostream>
- #include "worker.h"
- using namespace std;
- int main()
- {
- class Student student1(2017,"andrew",'m');
- student1.display();
- class Teacher teacher1(student1);
- teacher1.display();
- system("pause");
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。