C++学习代码
来源:互联网 发布:windows10怎么切换mac 编辑:程序博客网 时间:2024/06/16 19:13
1.重载运算符实现复数数据运算
#include<iostream>using namespace std;using std::cout;using std::endl;class Complex{private: double real; double image;public: Complex(double real=0.0,double image=0.0) { this->real=real;this->image=image; } void display() { cout<<"("<<real<<","<<image<<")"<<endl; } friend Complex operator +(Complex A,Complex B) { return Complex(A.real+B.real,A.image+B.image); } friend Complex operator -(Complex A,Complex B) { return Complex(A.real-B.real,A.image-B.image); } friend Complex operator -(Complex A) { return Complex(-A.real,-A.image); } friend Complex operator ++(Complex A) { return Complex(++A.real,A.image); } friend Complex operator ++(Complex A,int) { return Complex(A.real++,A.image); }};int main(){ int i; Complex A(100.0,200.0),B(-10.0,20.0),C; cout<<"A=", A.display(); cout<<"B=", B.display(); C=A+B; cout<<"C=A+B=",C.display(); C=A-B; cout<<"C=A-B=",C.display(); C=-A+B; cout<<"C=-A+B=",C.display(); C=A++; cout<<"C=A++=",C.display(); C=++A; cout<<"C=++A=",C.display(); C=A+5; cout<<"C=A+5=",C.display(); cin>>i;}
2.虚函数实现动态联编
虚函数可以让成员函数的操作一般化,基类的指针指向派生类对象时,用基类的指针访问虚成员函数,可访问指针真正指向对象的成员函数,而不是基类中的成员函数。如果没有定义为虚函数,则基类指针不管指向的是基类还是派生类对象,统一访问基类的成员函数。
#include<iostream>using namespace std;class Point{private: int X,Y;public: Point(int X=0,int Y=0) { this->X=X;this->Y=Y; } virtual double area() { return 0.0; }};const double PI=3.14159;class Circle:public Point{private: double radius;public: Circle(int X,int Y,double R):Point(X,Y) { radius=R; } double area() { return PI*radius*radius; }};int main(){ int i; Point P1(10,10); cout<<"P1.area()="<<P1.area()<<endl; Circle C1(10,10,20); cout<<"C1.area()="<<C1.area()<<endl; Point *pp; pp=&C1; cout<<"pp->area()="<<pp->area()<<endl; Point &rp=C1; cout<<"rp.area()="<<rp.area()<<endl; cin>>i;}
3.虚基类
为了解决从不同途径继承来的同名的数据成员在内存中有不同的拷贝造成数据不一致的问题,将共同基类设置为虚基类
在整个继承结构中,直接或简介继承虚基类的所有派生类,都必须在构造函数的初始化列表中列出对虚基类的初始化,但是,只有用于建立派生类对象的那个最远派生类的构造函数才调用虚基类的构造函数,而派生类的其它非虚基类中所列出的对这个虚基类的构造函数的调用被忽略,从而保证公共虚基类对象只初始化一次。
#include<iostream>using namespace std;class Automobile{private: int power;public: Automobile(int power) { this->power=power; cout<<"Automobile constructing..."<<endl; } void show() { cout<<" power:"<<power; }};class Car:virtual public Automobile{private: int seat;public: Car(int power,int seat):Automobile(power) { this->seat=seat; cout<<"Car constructing..."<<endl; } void show() { cout<<"car"; Automobile::show(); cout<<" seat:"<<seat<<endl; }};class Wagon:virtual public Automobile{private: int load;public: Wagon(int power,int load):Automobile(power) { this->load=load; cout<<"Wagon constructing..."<<endl; } void show() { cout<<"Wagon"; Automobile::show(); cout<<" load:"<<load<<endl; }};class StationWagon:public Car,public Wagon{public: StationWagon(int CPower,int WPower,int seat,int load):Automobile(CPower)\ ,Wagon(WPower,load),Car(CPower,seat) { cout<<"StationWagon constructing..."<<endl; } void show() { cout<<"StationWagon:"<<endl; Car::show(); Wagon::show(); }};int main(){ int i; StationWagon SW(105,108,3,8); SW.show(); cin>>i;}
- C代码!学习
- c代码学习
- [C++] C++学习代码
- RTT startup.c 代码学习
- C语言学习代码<一>
- C语言学习代码<三>
- c语言学习之代码
- Objective-C 代码块学习
- [C语言] C 语言学习代码
- c和c++代码精粹 学习笔记
- 学习C语言时候的代码
- C语言学习代码〈二〉
- C语言学习入门(视频+代码)
- C++Primer学习笔记(代码重用)
- C语言学习基础代码记录
- svm代码c++——机器学习
- Objective-C中代码块的学习
- C#/.net学习-7-飞行棋代码
- C# 和Java骨子里是两种不同的语言
- jquery内容过滤选择器、属性过滤选择器和jquery可见度过滤选择器
- bind1st bind2nd的使用
- java 之 读写锁 线程间互斥
- android不让程序显示在最近程序列表中
- C++学习代码
- wordpress 的filter钩子的动作名
- DirectVertex IVertexBuffer
- The Lottery Does More Harm than Good
- 浅谈JavaScript编程语言的编码规范
- 遇到错误:/usr/local/ld : can not find -lXrender (-lfontconfig -lXext )等等
- 你可能不知道的10个JavaScript小技巧
- Hibernate4 Annotation实例
- 设计模式——工厂模式