朱金付第十章编程
来源:互联网 发布:淘宝网童装店哪家好 编辑:程序博客网 时间:2024/06/07 01:38
1.
#include <iostream>using namespace std;class circle{public: virtual double funV(double r,double h=0){ return 3.1415926*r*r; }};class yuanZhu:public circle{public: double funV(double r,double h=0){ return 3.1415926*r*r*h; }};class yuanZhui:public circle{public: double funV(double r,double h=0){ return 3.1415926*r*r*h/3; }};int main(){ circle c,*p; yuanZhu z; yuanZhui zhui; p=&c; cout<<p->funV(100,0)<<endl; p=&z; cout<<p->funV(100,100)<<endl; p=&zhui; cout<<p->funV(100,300)<<endl; return 0;}
输出:
31415.9
3.14159e+06
3.14159e+06
2.
#include <iostream>using namespace std;class Complex{public: double real,Image; Complex(double r=0,double I=0){ real = r; Image = I; } Complex operator-(Complex x1); Complex &operator-=(Complex x2); bool operator!=(Complex x3); friend Complex operator+(Complex x4,Complex x5); friend Complex &operator+=(Complex &x6,Complex x7); friend bool operator==(Complex x8,Complex x9); void show(){ cout<<real; if (Image<0){ cout<<"-"<<Image<<"i"<<endl; }else{ cout<<"+"<<Image<<"i"<<endl; } }};Complex Complex::operator-(Complex x1){ Complex newX; newX.real = real - x1.real; newX.Image = Image - x1.Image; return newX;}Complex& Complex::operator-=(Complex x2){ real = real - x2.real; Image = Image - x2.Image; return *this;}bool Complex::operator!=(Complex x3) { if ((real!=x3.real)||(Image!=x3.Image)){ return true; }else{ return false; }}Complex operator+(Complex x4,Complex x5){ Complex newX; newX.real = x5.real + x4.real; newX.Image = x5.Image +x4.Image; return newX;}Complex& operator+=(Complex &x6,Complex x7){ x6.real = x6.real +x7.real; x6.Image = x6.Image + x7.Image; return x6;}bool operator==(Complex x8,Complex x9){ if ((x8.real==x9.real)&&(x8.Image==x9.Image)){ return true; }else{ return false; }}int main(){ Complex c1(1,1),c2(1,1),c3(1,1),c4(1,1),c5(1,1),c6(1,1),c7(1,1),c8(1,1),c9(1,1),c10(1,1),c11(1,1); c1=c2+c3;c1.show(); //2+2i c4+=c5;c4.show(); //2+2i c5=c6-c7;c5.show(); //0+0i c6-=c7;c6.show(); //0+0i cout<<(c8==c9)<<endl; //1 cout<<(c8!=c9)<<endl; //0 return 0;}
输出:
2+2i
2+2i
0+0i
0+0i
1
0
Process finished with exit code 0
3.定义一个三维坐标类,分别通过成员函数重载前置++和后置++运算符,通过友元函数重载前置--和后置--运算符,实现坐标值的+1和-1。main函数做测试。
#include <iostream>using namespace std;class coordinate{public: double x,y,z; coordinate(double x1=0,double y1=0,double z1=0){ x=x1;y=y1;z=z1; } coordinate operator++(); coordinate operator++(int); friend coordinate operator--(coordinate &pp); friend coordinate operator--(coordinate &pp,int); friend ostream& operator<<(ostream &os,coordinate tt);};/*成员函数实现++*/coordinate coordinate::operator++() { x++;y++;z++; return *this;}coordinate coordinate::operator++(int) { coordinate temp = *this; x++;y++;z++; return temp;}/*友元函数实现++*/coordinate operator--(coordinate &pp) { pp.x--;pp.y--;pp.z--; return pp;}coordinate operator--(coordinate &pp,int) { coordinate temp=pp; pp.x--;pp.y--;pp.z--; return temp;}ostream& operator<<(ostream& os,coordinate tt){ os<<"("<<tt.x<<","<<tt.y<<","<<tt.z<<")"<<endl; return os;}int main(){ coordinate c1(1,1,1),c2(1,1,1),c3(1,1,1),c4(1,1,1); cout<<(++c1); cout<<(c2++); cout<<(--c3); cout<<(c4--); return 0;}
4.
#include <iostream>#include <fstream>using namespace std;class student{public: double num,csBase,cpp,cppProject; char *name; student(double n=0,char p1[50]="姓名未知",double cB=0,double cp=0,double cpP=0){ num = n;csBase=cB;cpp=cp;cppProject=cpP; char *name = new char[50]; int i=0; while(p1[i]){ *name++=p1[i++]; } } double ave(){ double ave =(csBase+cpp+cppProject)/3 ; return ave; } friend istream &operator>>(istream &is,student &s1);};istream &operator>>(istream &is,student &s1){ cout<<"学号:";is>>s1.num; cout<<"姓名:姓名+空格:";cin.getline(s1.name,50,' '); cout<<"计算机基础:";is>>s1.csBase; cout<<"C++程序设计:";is>>s1.cpp; cout<<"C++课程设计:";is>>s1.cppProject; return is;}int main(){ student student1; cin>>student1; cout<<"平均成绩为"<<student1.ave(); return 0;}
阅读全文
0 0
- 朱金付第十章编程
- 第十章 多线程编程
- 第十章 并行编程
- 第十章 编程练习
- Windows Shell 编程 第十章
- Windows Shell 编程 第十章
- Python核心编程 第十章
- 第十章编程练习(1)
- 第十章编程练习(2)
- 第十章编程练习(3)
- 第十章编程练习(4)
- 第十章编程练习(5)
- 第十章编程练习(6)
- 第十章编程练习(7)
- python 核心编程 第十章
- 【c++编程思想】第十章 名字控制
- 【编程珠玑】第十章 节省空间
- unix网络编程-第十章-小结
- Objective-C 判断对象是否相等
- curl编译以及arm交叉编译
- RocketMQ(5)——消息文件过期原理
- 贝叶斯分类器原理与应用
- DFS专练3 全排列字典序输出问题
- 朱金付第十章编程
- mac系统从移动硬盘往外考资料时提示“不能更改xx中的一个或多个项目,因为它们正在使用中”压根就没开始用
- Proper Nutrition
- Python 字符串 循环
- Qt与SQL server的连接使用
- 什么是spring boot
- RocketMQ(6)——客户端配置详解
- Gensim官方教程翻译(一)——语料库与向量空间(Corpora and Vector Spaces)
- Gym 101617J dp