C++-----编程实战(一)
来源:互联网 发布:淘宝的隐形眼镜靠谱吗 编辑:程序博客网 时间:2024/05/29 05:55
作用:处理对象的初始化操作。
#include <iostream>using namespace std;enum CPU_Rank {P1=1,P2,P3,P4,P5}; //枚举类型class CPU{private: CPU_Rank Rank; int fre; float volt;public: CPU(CPU_Rank r, int f, float v); //普通的定义法 void Get_CPU() const; //常成员函数 ~CPU(){cout<<"析构一个CPU"<<endl;} //析构函数};CPU::CPU(CPU_Rank r, int f, float v){ Rank = r; fre = f; volt = v; cout<<"构造一个CPU"<<endl;} void CPU::Get_CPU() const //常成员函数实现格式 { cout<<Rank<<" "<<fre<<" "<<volt<<" "<<endl; } int main() { CPU c(P5,5,2.3); c.Get_CPU(); return 0; }
#include <iostream>using namespace std;enum CPU_Rank {P1=1,P2,P3,P4,P5}; //枚举类型 class CPU { private: CPU_Rank Rank; int fre; float volt; public: CPU(CPU_Rank R = P1,int f = 5, float v = 3.0) //构造函数参数赋初值 { Rank = R; fre = f; volt = v; } void Get_CPU()const { cout<<Rank<<" "<<fre<<" "<<volt<<" "<<endl; } }; int main() { CPU c; c.Get_CPU(); return 0; }
#include <iostream>using namespace std;enum CPU_Rank {P1=1,P2,P3,P4,P5}; //枚举类型 class CPU { private: CPU_Rank Rank; int fre; float volt; public: CPU(CPU_Rank R = P1,int f = 5, float v = 3.0):Rank(R),fre(f),volt(v){} //参数初始化列表,注意格式 void Get_CPU()const { cout<<Rank<<" "<<fre<<" "<<volt<<" "<<endl; } }; int main() { CPU c; c.Get_CPU(); return 0; }
#include <iostream>using namespace std; class Dog { private: string name; int age; float weight; public: Dog(string n = "blue",int a =1, float w =12 ): name(n),age(a),weight(w){} ~Dog(){cout<<"调用析构函数!";} Set_Name(int na) { name = na; } Set_Abge(int ag) { age = ag; } Set_Weight(int we) { weight = we; } Dog_Show()const { cout<<"The name :"<< name<<" " <<"Age:"<<age<<" " <<"Weight: "<<weight<<endl; } }; int main() { Dog d; d.Dog_Show(); return 0; }
例子:设计一个类--圆,输入半径,根据半径计算矩形面积。
#include<iostream>using namespace std; class Circle { public: Circle(float r):radius(r){} float Area()const { return 3.14*radius*radius; } private: float radius; }; int main() { float radius; cout<<"please input the radius: "<<endl; cin>>radius; Circle p(radius); cout<<"The Area is: "<<p.Area()<<endl; }
例子:定义一个复数类,可以实现如下功能:
用3+5i 初始化c1
用实数 4.5 初始化 c2
c1和c2的值相加,结果保存在 c1中,然后输出。
#include <iostream>using namespace std;class Complex{public: Complex(double r, double i):real(r),image(i){} void add(Complex c) { real += c.real; image += c.image; } void show()const { cout<<"The add : "<<real <<"+"<<image<<"i"<<endl; }private: double real; double image;};int main(){ Complex c1(3,5),c2(4.5,0); c1.add(c2); c1.show();}
例子:两个长方体,长宽高(12,20,30),(10,20,30),求体积。
要求:
基于对象
定义两个构造函数,一个有参数,一个无参数。
#include <iostream>using namespace std;class Box{private: int length; int width; int height;public: Box():length(12),width(20),height(25){}; Box(int l,int w,int h):length(l),width(w),height(h){}; int volume();};int Box::volume(){ cout<<"The volume :" <<length*width*height<<endl;}int main(){ Box b; b.volume(); Box b1(10,30,20); b1.volume();}例子:
定义一个模板类,利用它分别实现两个整数,浮点数,字符实现比较大小。
#include <iostream>using namespace std;template<class numtype> //模板类格式class Compare{public: Compare(numtype a,numtype b):x(a),y(b){}; numtype maxValue() const {return (x>y)? x: y;} numtype minValue() const {return (x<y)? x: y;}private: numtype x,y;};int main(){ Compare<int>c1(2,3); cout<<"The max :"<< c1.maxValue()<<endl; cout<<"The max :"<< c1.minValue()<<endl; Compare<double>c2(2.8,3.7); cout<<"The max :"<< c2.maxValue()<<endl; cout<<"The max :"<< c2.minValue()<<endl; Compare<string>c3("sss","rtr"); cout<<"The max :"<< c3.maxValue()<<endl; cout<<"The max :"<< c3.minValue()<<endl; return 0;}
例子:建立一个对象数组,内放 5 个学生的数据(学号、成绩),设立一个函数 max ,用指向对象的指针作函数参数,在 max函数中找出 5 个学生中成绩最高者,并输出其学号。初值自拟。
#include <iostream>using namespace std;class Student{public: //后面在函数外面需要引用成员函数,所以是公有成员函数 int num; int score; Student(int n,float s):num(n),score(s){}};int maxF(Student *arr) { int j=0; float sigValue = arr[0].score; for(int i=0; i<6; i++) { if(arr[i].score>sigValue) { j = i; sigValue = arr[i].score; } } return j; }int main(){ Student stud[6]={ Student(1000,79), Student(1001,79), Student(1002,70), Student(1003,96), Student(1004,94), Student(1005,92), }; int maxn= maxF(stud); cout<<stud[maxn].num<<" "<<stud[maxn].score<<endl; return 0;}
对象是类的具体化,而类是对象的抽象化,对象在使用前必须先定义或先创建。源自于同一个类的对象必然具有想同的数据成员和成员函数。而不同类的对象可以有想同的操作。同一类的不同对象具有的操作也应该是想同的。
0 0
- C++-----编程实战(一)
- linux c 编程实战:文件操作总结(一)
- linux c 编程实战:进程控制总结(一)
- C++---编程实战(二)
- C语言实战(一)
- 《LINUX C编程实战》自学笔记——套接字编程(一)
- Linux-C成长之路(一)Linux C编程实战之路 Linux下C编程概要
- Linux下的C编程实战(一)----开发平台搭建
- LINUX下的C编程实战(一)――开发平台搭建
- Linux下的C编程实战(一)――开发平台搭建
- 【Java并发编程实战】-----“J.U.C”:AQS分析(一)
- 实战体会Java多线程编程(一)
- java多线程编程 - 实战篇(一)
- VC++ 实战OLEDB编程(一)
- VC++ 实战数据库OLEDB编程(一)
- Java并发编程实战笔记(一)
- 链表编程实战(一):单链表
- VC++ 实战OLEDB编程(一)
- Android studio Android Monitor介绍三
- 读取Jar包中的资源问题探究
- redis秒杀系统数据同步(保证不多卖)
- Android studio Android Monitor介绍四
- drawContours函数注意事项
- C++-----编程实战(一)
- Unity3D-UGUI-带滚动条的滑动文本框
- JS面向对象编程--第三章 函数
- 第79篇Elasticsearch搜索引擎研究及array_walk_recursive及arsort
- windows详细配置gitblit
- struts2多个文件上传
- DBus 的 C 编程接口
- Java线程安全问题与同步锁
- 公众平台商户申请步骤(公众号、扫码、刷卡支付)