作用:处理对象的初始化操作。
#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;
}
对象是类的具体化,而类是对象的抽象化,对象在使用前必须先定义或先创建。源自于同一个类的对象必然具有想同的数据成员和成员函数。而不同类的对象可以有想同的操作。同一类的不同对象具有的操作也应该是想同的。