最近要复习一下C和C++的基础知识,于是计划把之前学过的谭浩强的《C程序设计》和《C++程序设计》习题重新做一遍。
编译环境为:操作系统32位Win7,编译工具VC++6.0
第十章:运算符重载
10.1-2)定义一个复数类Complex,重载运算符'+'、'-'、'*'、'/',使之能用于复数的加、减、乘以、除以运算
#includeusing namespace std;class Complex{public: //构造函数 Complex(double r = 0, double i = 0) { this -> m_Real = r; this -> m_Imag = i; } //打印复数 void Display() { cout << m_Real << "+" << m_Imag << "i" << endl; } Complex operator + (Complex &c2); Complex operator - (Complex &c2); Complex operator * (Complex &c2); Complex operator / (Complex &c2); double Real() { return m_Real; } //获取实部 double Imag() { return m_Imag; } //获取虚部 void SetReal(double r) { m_Real = r; } //设置实部 void SetImag(double i) { m_Imag = i; } //设置虚部 void SetValue(double r, double i) { m_Real = r; m_Imag = i; } //设置复数值private: double m_Real; //实部 double m_Imag; //虚部};Complex Complex :: operator + (Complex &c2){ Complex c = Complex(); c.SetReal(m_Real + c2.Real()); c.SetImag(m_Imag + c2.Imag()); return c;}Complex Complex :: operator - (Complex &c2){ Complex c = Complex(); c.SetReal(m_Real - c2.Real()); c.SetImag(m_Imag - c2.Imag()); return c;}Complex Complex :: operator * (Complex &c2){ Complex c = Complex(); c.SetReal(m_Real * c2.Real() - m_Imag * c2.Imag()); c.SetImag(m_Real * c2.Imag() + m_Imag * c2.Real()); return c;}Complex Complex :: operator / (Complex &c2){ Complex c = Complex(); double da = m_Real; double db = m_Imag; double dc = c2.Real(); double dd = c2.Imag(); c.SetReal((da * dc + db * dd) / (dc * dc + dd * dd)); c.SetImag((db * dd - da * dc) / (dc * dc + dd * dd)); return c;}int main(){ cout << "两个复数" << endl; Complex(1, 1).Display(); Complex(2, 3).Display(); cout << "复数加法" << endl; Complex c1 = Complex(1, 1) + Complex(2, 3); c1.Display(); //和 cout << "复数减法" << endl; Complex c2 = Complex(1, 1) - Complex(2, 3); c2.Display(); //差 cout << "复数乘法" << endl; Complex c3 = Complex(1, 1) * Complex(2, 3); c3.Display(); //积 cout << "复数除法" << endl; Complex c4 = Complex(1, 1) / Complex(2, 3); c4.Display(); //商 return 0;}
10.3)定义一个复数类Complex,重载运算符+,使之能用于复数和整数的加法运算
#includeusing namespace std;class Complex{public: //构造函数 Complex(double r = 0, double i = 0) { this -> m_Real = r; this -> m_Imag = i; } //打印复数 void Display() { cout << m_Real << "+" << m_Imag << "i" << endl; } Complex operator + (Complex &c2); Complex operator + (int a); double Real() { return m_Real; } //获取实部 double Imag() { return m_Imag; } //获取虚部 void SetReal(double r) { m_Real = r; } //设置实部 void SetImag(double i) { m_Imag = i; } //设置虚部 void SetValue(double r, double i) { m_Real = r; m_Imag = i; } //设置复数值private: double m_Real; //实部 double m_Imag; //虚部};//复数加复数Complex Complex :: operator + (Complex &c2){ Complex c = Complex(); c.SetReal(m_Real + c2.Real()); c.SetImag(m_Imag + c2.Imag()); return c;}//复数加整数Complex Complex :: operator + (int a){ Complex c = Complex(); c.SetReal(m_Real + a); c.SetImag(m_Imag); return c;}//整数加复数Complex operator + (int a, Complex &c2){ Complex c = Complex(); c.SetReal(a + c2.Real()); c.SetImag(c2.Imag()); return c;}int main(){ cout << "两个复数" << endl; Complex(1, 1).Display(); Complex(2, 3).Display(); cout << "复数加复数" << endl; Complex c1 = Complex(1, 1) + Complex(2, 3); c1.Display(); //和 cout << "复数加整数" << endl; Complex c2 = Complex(1, 1) + 7; c2.Display(); //和 cout << "整数加复数" << endl; Complex c3 = 7 + Complex(2, 3); c3.Display(); //和 return 0;}
10.4)有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符+。使之能用于矩阵相加。
#includeusing namespace std;class Matrix{public: //构造函数 Matrix(int a00, int a01, int a02, int a10, int a11, int a12) { this -> m_Matrix[0][0] = a00; this -> m_Matrix[0][1] = a01; this -> m_Matrix[0][2] = a02; this -> m_Matrix[1][0] = a10; this -> m_Matrix[1][1] = a11; this -> m_Matrix[1][2] = a12; } //2行3列的矩阵 int m_Matrix[2][3];};//矩阵加法Matrix operator + (Matrix &m1, Matrix &m2){ return Matrix( m1.m_Matrix[0][0] + m2.m_Matrix[0][0], m1.m_Matrix[0][1] + m2.m_Matrix[0][1], m1.m_Matrix[0][2] + m2.m_Matrix[0][2], m1.m_Matrix[1][0] + m2.m_Matrix[1][0], m1.m_Matrix[1][1] + m2.m_Matrix[1][1], m1.m_Matrix[1][2] + m2.m_Matrix[1][2]);}int main(){ Matrix a = Matrix(1, 2, 3, 4, 5, 6); Matrix b = Matrix(6, 5, 4, 3, 2, 1); Matrix c = a + b; cout << c.m_Matrix[0][0] << c.m_Matrix[0][1] << c.m_Matrix[0][2] << endl << c.m_Matrix[1][0] << c.m_Matrix[1][1] << c.m_Matrix[1][2] << endl; return 0;}
10.5)在4题的基础上,重载流插入运算符<<和流提取运算符>>,使之能用于矩阵的输入和输出
#includeusing namespace std;class Matrix{public: //构造函数 Matrix(int a00, int a01, int a02, int a10, int a11, int a12) { this -> m_Matrix[0][0] = a00; this -> m_Matrix[0][1] = a01; this -> m_Matrix[0][2] = a02; this -> m_Matrix[1][0] = a10; this -> m_Matrix[1][1] = a11; this -> m_Matrix[1][2] = a12; } //2行3列的矩阵 int m_Matrix[2][3];};ostream& operator << (ostream& output, Matrix& m){ output << m.m_Matrix[0][0] << " " << m.m_Matrix[0][1] << " " << m.m_Matrix[0][2] << " " << endl << m.m_Matrix[1][0] << " " << m.m_Matrix[1][1] << " " << m.m_Matrix[1][2]; return output;}istream& operator >> (istream& input, Matrix& m){ input >> m.m_Matrix[0][0] >> m.m_Matrix[0][1] >> m.m_Matrix[0][2] >> m.m_Matrix[1][0] >> m.m_Matrix[1][1] >> m.m_Matrix[1][2]; return input;}int main(){ Matrix a = Matrix(0, 0, 0, 0, 0, 0); cin >> a; cout << a << endl; return 0;}
10.6)编写程序实现复数类与浮点数见的强制类型转换
#includeusing namespace std;class Complex{public: //构造函数 Complex(double r = 0, double i = 0) { this -> m_Real = r; this -> m_Imag = i; } //复数转浮点数 operator double() { return m_Real; } //打印复数 void Display() { cout << m_Real << "+" << m_Imag << "i" << endl; } double Real() { return m_Real; } //获取实部 double Imag() { return m_Imag; } //获取虚部 void SetReal(double r) { m_Real = r; } //设置实部 void SetImag(double i) { m_Imag = i; } //设置虚部 void SetValue(double r, double i) { m_Real = r; m_Imag = i; } //设置复数值private: double m_Real; //实部 double m_Imag; //虚部};int main(){ Complex c1 = Complex(1, 1); c1.Display(); double temp = (double)c1; cout << temp << endl; Complex c2 = (Complex)temp; c2.Display(); return 0;}
10.7)定义两个类间的强制类型转换
#include#include using namespace std;class Teacher{public: Teacher(int nu, string na, bool sx, int o1 = 0, int o2 = 0, int o3 = 0) { this -> m_Num = nu; this -> m_Name = na; this -> m_Sex = sx; this -> m_Other1 = o1; this -> m_Other2 = o2; this -> m_Other3 = o3; } //输出教师类内容 void Display() { cout << m_Num << " " << m_Name << " " << m_Sex << endl; cout << m_Other1 << "-" << m_Other2 << "-" << m_Other3 << endl; }private: int m_Num; //号码 string m_Name;//姓名 bool m_Sex; //性别:1男0女 int m_Other1; //其他属性1 int m_Other2; //其他属性2 int m_Other3; //其他属性3};class Student{public: //构造函数 Student(int nu, string na, bool sx) { this -> m_Num = nu; this -> m_Name = na; this -> m_Sex = sx; } operator Teacher() { return Teacher(m_Num, m_Name, m_Sex); }private: int m_Num; //号码 string m_Name;//姓名 bool m_Sex; //性别:1男0女};int main(){ Student stu = Student(100, "Tsybius", 1); Teacher tea = (Teacher)stu; tea.Display(); return 0;}
第十一章:继承与派生
11.1-4)修改例11.1、11.2、11.3的程序,分别使用public、private、protected三种继承方式
#include#include using namespace std;class Student{public: Student(int nu, string na, char sx) { this -> m_Num = nu; this -> m_Name = na; this -> m_Sex = sx; } void GetValue() { cin >> m_Num >> m_Name >> m_Sex; } void Display() { cout << "m_Num:" << m_Num << endl; cout << "m_Name:" << m_Name << endl; cout << "m_Sex:" << m_Sex << endl; }private: int m_Num; string m_Name; char m_Sex;};//在这里修改 public private protectedclass Student1: public Student //11.2//class Student1: private Student //11.3//class Student1: protected Student //11.4{public: Student1( int nu = 100, string na = "Tsybius", char sx = 'm', int ag = 23, string ad = "x"): Student(nu, na, sx) { this -> m_Age = ag; this -> m_Addr = ad; } void Display1() { Display(); cout << "m_Age:" << m_Age << endl; cout << "m_Addr:" << m_Addr << endl; }private: int m_Age; string m_Addr;};int main(){ int a; string b; char c; int d; string e; cin >> a >> b >> c >> d >> e; //cout << a << b << c << d << e << endl; Student1 stu1 = Student1(a, b, c, d, e); stu1.Display1(); return 0;}
11.9)分别定义教师类(Teacher)和干部类(Cadre),采用多重继承方式派生新类Teacher_Cadre
#include#include using namespace std;//教师类class Teacher{public: //构造函数 Teacher(string nm, int ag, char sx, string ad, string tl, string tt); //输出内容 void Display();protected: string m_Name; //姓名 int m_Age; //年龄 char m_Sex; //性别 'm'男 'f'女 string m_Addr; //地址 string m_Tel; //电话 string m_Title; //职称};//教师类:构造函数Teacher :: Teacher(string nm, int ag, char sx, string ad, string tl, string tt): m_Name(nm), m_Age(ag), m_Sex(sx), m_Addr(ad), m_Tel(tl), m_Title(tt) { }//输出内容void Teacher :: Display(){ cout << "Teacher_Name:" << m_Name << endl; cout << "Teacher_Age:" << m_Age << endl; cout << "Teacher_Sex:" << m_Sex << endl; cout << "Teacher_Addr:" << m_Addr << endl; cout << "Teacher_Tel:" << m_Tel << endl; cout << "Teacher_Title:" << m_Title << endl;}//干部类class Cadre{public: //构造函数 Cadre(string nm, int ag, char sx, string ad, string tl, string pst); //输出内容 void Display();protected: string m_Name; //姓名 int m_Age; //年龄 char m_Sex; //性别 'm'男 'f'女 string m_Addr; //地址 string m_Tel; //电话 string m_Post; //职务};//干部类:构造函数Cadre :: Cadre(string nm, int ag, char sx, string ad, string tl, string pst): m_Name(nm), m_Age(ag), m_Sex(sx), m_Addr(ad), m_Tel(tl), m_Post(pst) { }//输出内容void Cadre :: Display(){ cout << "Cadre_Name:" << m_Name << endl; cout << "Cadre_Age:" << m_Age << endl; cout << "Cadre_Sex:" << m_Sex << endl; cout << "Cadre_Addr:" << m_Addr << endl; cout << "Cadre_Tel:" << m_Tel << endl; cout << "Cadre_Post:" << m_Post << endl;}class Teacher_Cadre: public Teacher, public Cadre{public: //构造函数 Teacher_Cadre( string na, int ag, char sx, string ad, string tl, string tt, string pst, int wg); //输出内容 void Display();private: int m_Wages; //工资};//教师干部类:构造函数Teacher_Cadre :: Teacher_Cadre( string na, int ag, char sx, string ad, string tl, string tt, string pst, int wg): Teacher(na, ag, sx, ad, tl, tt), Cadre(na, ag, sx, ad, tl, pst), m_Wages(wg) { }//输出内容void Teacher_Cadre :: Display(){ Teacher :: Display(); Cadre :: Display(); cout << "TC_Wages:" << m_Wages << endl;}int main(){ Teacher_Cadre tc = Teacher_Cadre("Tsybius", 23, 'm', "aa", "bb", "cc", "dd", 1000); tc.Display(); return 0;}
11.10)将11.8节程序片段补充完善,成为一个完整的程序。在程序中使用继承和组合
#include#include using namespace std;//教师类class Teacher{public: //构造函数 Teacher(int nu, string na, char sx): m_Num(nu), m_Name(na), m_Sex(sx) { } //输出教师类 void Display() { cout << "Teacher_Num:" << m_Num << endl; cout << "Teacher_Name:" << m_Name << endl; cout << "Teacher_Sex:" << m_Sex << endl; }private: int m_Num; //号码 string m_Name; //姓名 char m_Sex; //性别 'm'男'f'女};//生日类class BirthDate{public: //构造函数 BirthDate(int y, int m, int d): m_Year(y), m_Month(m), m_Day(d) { } BirthDate(BirthDate &bd) { this -> m_Year = bd.m_Year; this -> m_Month = bd.m_Month; this -> m_Day = bd.m_Day; } //输出生日类 void Display() { cout << "Teacher_Year:" << m_Year << endl; cout << "Teacher_Month:" << m_Month << endl; cout << "Teacher_Day:" << m_Day << endl; }private: int m_Year; //年 int m_Month; //月 int m_Day; //日};//教授类class Professor: public Teacher{public: //构造函数 Professor(int nu, string na, char sx, int y, int m, int d): Teacher(nu, na, sx), birthdate(y, m, d) { } //输出教授类 void Display() { Teacher :: Display(); birthdate.Display(); }private: BirthDate birthdate;};int main(){ (new Professor(100, "Tsybius", 'm', 2014, 9, 16)) -> Display(); return 0;}
第十二章:多态性与虚函数
12.1)在例12.1的基础上做一些修改,定义Point类,由Point类派生出Circle类,再有Circle类派生出Cylinder类
#includeusing namespace std;//点类class Point{public: Point(float x = 0, float y = 0); //构造函数 void SetPoint(float x = 0, float y = 0); //为点重新赋值 float GetX(); //获取横坐标 float GetY(); //获取纵坐标 //运算符重载 friend ostream& operator << (ostream &, const Point &);private: float m_X; float m_Y;};//构造函数Point :: Point(float a, float b){ this -> m_X = a; this -> m_Y = b;}//为点重新赋值void Point :: SetPoint(float a, float b){ this -> m_X = a; this -> m_Y = b;}//横坐标float Point :: GetX(){ return this -> m_X;}//纵坐标float Point :: GetY(){ return this -> m_Y;}//重载运算符ostream& operator << (ostream &output, Point &p){ output << "[" << p.GetX() << "," << p.GetY() << "]"; return output;}//==================================================//圆类class Circle: public Point{public: Circle(float x, float y, float r): Point(x, y), m_Radius(r) { } //构造函数 float GetRadius() { return this -> m_Radius; } //获取半径 float GetPerimeter() { return 2 * 3.1416f * m_Radius; } //获取周长 float GetArea() { return 3.1416f * m_Radius * m_Radius; } //获取面积 //运算符重载 friend ostream& operator << (ostream &, const Circle &);private: float m_Radius; //半径};//重载运算符ostream& operator << (ostream &output, Circle &c){ output << "[" << c.GetX() << "," << c.GetY() << "] r=" << c.GetRadius(); return output;}//==================================================//圆柱体类class Cylinder: public Circle{public: Cylinder(float x, float y, float r, float h): Circle(x, y, r), m_Height(h) { } float GetHeight() { return this -> m_Height; } float GetVolume() { return Circle :: GetArea() * m_Height; } //运算符重载 friend ostream& operator << (ostream &, const Cylinder &);private: float m_Height; //高};//重载运算符ostream& operator << (ostream &output, Cylinder &cy){ output << "[" << cy.GetX() << "," << cy.GetY() << "]" << " r=" << cy.GetRadius() << " h=" << cy.GetHeight(); return output;}int main(){ Point p(3.5f, 6.4f); cout << p << endl; Circle c(3.5f, 6.4f, 3); cout << c << endl; Cylinder cy(3.5f, 6.4f, 3, 5); cout << cy << endl; return 0;}
12.3)在例12.3基础上做如下修改
1:把构造函数修改为带参数的函数
#includeusing namespace std;class Point{public: //构造函数 Point(float x = 0, float y = 0): m_X(x), m_Y(y) { } //析构函数 ~Point() { cout << "Executing Point Destructor" << endl; } //virtual ~Point() { cout << "Executing Point Destructor" << endl; } float GetX() { return this -> m_X; } //获取横坐标 float GetY() { return this -> m_Y; } //获取纵坐标private: int m_X; //横坐标 int m_Y; //纵坐标};class Circle: public Point{public: //构造函数 Circle(float x = 0, float y = 0, float r = 1): Point(x, y), m_Radius(r) { } //析构函数 ~Circle() { cout << "Executing Circle Destructor" << endl; } float GetRadius() { return this -> m_Radius; }private: int m_Radius; //半径};int main(){ Point *p = new Circle(); delete p; cout << "---------" << endl; Circle * gradl = new Circle(); delete gradl; cout << "---------" << endl; return 0;}
2:不将析构函数声明为virtual,运行结果:
3:不进行2中的修改
12.4)写一个程序,定义抽象基类Shape,由它派生出三个类:Circle、Rectangle、Triangle,用一个函数PrintArea分别输出三者的面积
#include#include using namespace std;class Shape{public: virtual void PrintArea() { }};//圆class Circle: public Shape{public: Circle(float r): m_Radius(r) { } void PrintArea() { cout << 3.1416 * m_Radius * m_Radius << endl; }private: float m_Radius;};//矩形class Rectangle: public Shape{public: Rectangle(float l, float w): m_Length(l), m_Width(w) { } void PrintArea() { cout << m_Length * m_Width << endl; }private: float m_Length; //长 float m_Width; //宽};//三角形class Triangle: public Shape{public: Triangle(float a, float b, float c): m_A(a), m_B(b), m_C(c) { } void PrintArea() { float p = (m_A + m_B + m_C) / 2.0f; float result = sqrt(p * (p - m_A) * (p - m_B) * (p - m_C)); cout << result << endl; }private: float m_A; //边1 float m_B; //边2 float m_C; //边3};int main(){ (new Circle(5)) -> PrintArea(); (new Rectangle(2, 3)) -> PrintArea(); (new Triangle(3, 4, 5)) -> PrintArea(); return 0;}
12.5)写一个程序,定义抽象基类Shape,由他派生出5个派生类:Circle、Square、Rectangle、Trapezoid、Triangle。用虚函数分别计算几种图形的面积并求和。要求使用基类指针数组,使它的每一个元素指向一个派生类对象
#include#include using namespace std;class Shape{public: virtual void PrintArea() { }};//圆class Circle: public Shape{public: Circle(float r): m_Radius(r) { } virtual void PrintArea() { cout << "Circle:" << 3.1416 * m_Radius * m_Radius << endl; }private: float m_Radius;};//正方形class Square: public Shape{public: Square(float a): m_A(a) { } virtual void PrintArea() { cout << "Square:" << m_A * m_A << endl; }private: float m_A;};//矩形class Rectangle: public Shape{public: Rectangle(float l, float w): m_Length(l), m_Width(w) { } virtual void PrintArea() { cout << "Rectangle:" << m_Length * m_Width << endl; }private: float m_Length; //长 float m_Width; //宽};//梯形class Trapezoid: public Shape{public: Trapezoid(float a, float b, float h): m_A(a), m_B(b), m_Height(h) { } virtual void PrintArea() { cout << "Trapezoid:" << m_A * m_B * m_Height / 2.0f << endl; }private: float m_A; //上底 float m_B; //下底 float m_Height; //高};//三角形class Triangle: public Shape{public: Triangle(float a, float b, float c): m_A(a), m_B(b), m_C(c) { } virtual void PrintArea() { float p = (m_A + m_B + m_C) / 2.0f; float result = sqrt(p * (p - m_A) * (p - m_B) * (p - m_C)); cout << "Triangle:" << result << endl; }private: float m_A; //边1 float m_B; //边2 float m_C; //边3};int main(){ Shape *s[5]; s[0] = new Circle(5); s[0] -> PrintArea(); s[1] = new Square(5); s[1] -> PrintArea(); s[2] = new Rectangle(2, 3); s[2] -> PrintArea(); s[3] = new Trapezoid(2, 3, 4); s[3] -> PrintArea(); s[4] = new Triangle(3, 4, 5); s[4] -> PrintArea(); return 0;}
END