C ++ classes and objects are the first characteristic linguistic differences with the C language, is essential for white new to C ++ language is.
1. On:
Experiment 1:
Design of a circle class Circle, class data members have the center coordinates (x, y), the radius r, also has the following functions:
(1) initialize the center coordinates and the radius
(2) and the display center coordinates radius
(3) calculate the area of a circle
in the main function, defined in the class declaration test object.
#include<iostream>
using namespace std;
class Circle
{
private:
int x,y,r;
double s;
public:
void init(int xx,int yy,int rr)//初始化,赋值init。
{
x=xx;
y=yy;
r=rr;
}
void print()//输出 圆心(2,2),半径3。
{
cout<<"圆心("<<x<<","<<y<<"),半径"<<r<<endl;
}
double area()//计算圆面积,不是输出。
{
return 3.14*r*r;
}
};
int main()
{
Circle c1;
//测试。
c1.init(2,2,3);//初始化函数必须放在最开始的位置。
c1.print();
cout<<c1.area()<<endl;
system("pause");
return 0;
}
Experiment 2:
Design a cuboid class Box. This class has data members comprises a length (length), width (width), high (H). Requires member function following functions:
(1) initialize the cuboid long, wide and high;
(2) calculating the volume of a rectangular parallelepiped;
(3) A box of length, width and height.
In the main function, defined test objects declared the class.
#include<iostream>
using namespace std;
class Box
{
private:
int x,y,z,v;
public:
void init(int xx,int yy,int zz)//初始化,赋值init。
{
x=xx;
y=yy;
z=zz;
}
void print()//输出长方体的长宽高。
{
cout<<"长:"<<x<<"宽:"<<y<<"高:"<<z<<endl;
}
double volume()//计算长方体的体积,不是输出。
{
return x*y*z;
}
};
int main()
{
Box c1;
//测试。
c1.init(2,2,3);//初始化函数必须放在最开始的位置。
c1.print();
cout<<c1.volume()<<endl;
system("pause");
return 0;
}
2. Construction and destructor + arrays of objects and object pointers:
moving objects 3. establish, release and assignment, copy static member function +:
Experiment 3:
call the constructor and the case when the object destructors Effect Analysis:
#include<iostream>
using namespace std;
class Circle
{
private:
int x,y,r;
public:
Circle(int x,int y,int r)
{
this->x=x;
this->y=y;
this->r=r;
cout<<"调用了有参构造函数"<<r<<'\t';
}
Circle()
{
x=0;
y=0;
r=0;
cout<<"调用了无参构造函数"<<r<<'\t';
}
Circle(Circle &c)
{
this->x =c.x;
this->y=c.y;
this->r =c.r;
cout<<"调用了复制构造函数"<<r<<'\t';
}
~Circle()
{
cout<<"调用了析构函数"<<r<<'\t';
}
};
int main()
{
Circle c1;
cout<<endl<<endl;
Circle c2(1,1,1);
cout<<endl<<endl;
Circle *pc1=new Circle;
cout<<endl<<endl;
Circle *pc2=new Circle();
cout<<endl<<endl;
Circle *pc3=new Circle(3,3,3);
cout<<endl<<endl;
Circle *pc4=new Circle[3];
cout<<endl<<endl;
Circle c3;
c3=c2;
Circle c4(c3);
cout<<endl<<endl;
delete pc1;
cout<<endl<<endl;
delete pc2;
cout<<endl<<endl;
delete pc3;
cout<<endl<<endl;
delete []pc4;
cout<<endl<<endl;
return 0;
}
4. Function and meta friend classes:
Experiment 4:
Statement plurality Complex class, there are two data members real real, the imaginary part imag. This class has the following functions:
(1) constructor initializes a real part and an imaginary part;
(2) 2 complex additions implemented operations add (Complex) function, implemented using the class member functions;
(3) implement 2 complex subtraction sub (complex, complex) function, the UF-membered function implementation;
the main function, defined in the class declaration test object.
#include<iostream>
using namespace std;
class Complex
{
private:
int real,imag;
public:
Complex (int r,int i){real=r;imag=i;}//有参
Complex (){real=0,imag=0;}//无参
Complex add(Complex c2)//c3=c1+c2
{
Complex c;
c.real=this->real+c2.real ;
c.imag=this->imag+c2.imag;
return c;
}
friend Complex sub(Complex c1,Complex c2)//c4=c1-c2
{
Complex t=Complex(c1.real-c2.imag,c1.real-c2.imag );
return t;
}
void print()//a+bi
{
cout<<real;
if(imag>=0)
cout<<"+";
cout<<imag<<"i"<<endl;
}
};
int main()
{
Complex c1(1,1),c2(2,2),c3,c4,*t;
t=new Complex;
*t=sub(c1,c2);
t->print();
c3=c1.add(c2);//c3=c1+c2
c4=sub(c1,c2);//c4=c1-c2
c3.print ();
c4.print ();
cout<<endl;
system("pause");
return 0;
}
下面我们来做一个类和对象的综合应用的实验来结束这一特性的阐述:
实验5:
设计一个日期Date类,私有成员年(year),月(month),日(Day)。实现以下功能:
(1)计算一个日期后多少天的日期,如2018年5月1日后5天是2018年5月6日
(2)计算两个日期间相差多少天,如2018年5月1日与2018年6月2日间相差32天。
在主函数中,定义对象来测试该类的声明。
#include <iostream>
#include<math.h>
using namespace std;
class Date
{
private:
int year,month,day;
int isLeapYear();
int isLeapYear(int year);
int isEndOfMonth();
void addOneDay();
int calculateDays();
public:
Date(int y,int m,int d);
Date();
void print();
Date addDays(int days);
friend Date addDays(Date d,int days);
friend int daysOf2Date(Date d1,Date d2);
int daysOf2Date(Date d2);
};
int Date::calculateDays()//2019.4.23 1900.1.1 d2.calculateDays();
{
int days=0;
days=(year-1900)*365;//年119*365 +1+1
for(int y=1900;y<year;y++){ if(isLeapYear(y)){days++;}}
switch(month)//2019.10.23 1900.1.1
{
case 12:days=days+30;//11月的天数
case 11:days=days+31;//10月的天数
case 10:days=days+30;//9月的天数
case 9:days=days+31;//8月的天数
case 8:days=days+31;//7月的天数
case 7:days=days+30;//6月的天数
case 6:days=days+31;//5月的天数
case 5:days=days+30;//4月的天数
case 4:days=days+31;//3月的天数
case 3:{if(isLeapYear()){days=days+29;}else {days=days+28;}}//2月的天数
case 2:days=days+31;//1月的天数
}//月4-1=3 31+29/28+31
days=days+day-1;//日 23-1
return days;
}
void Date::addOneDay()
{
if(isEndOfMonth()==1)
{
if(month==12){year++;month=1;day=1;}
else{month++;day=1;}
}
else{day++;}
}
int Date::isEndOfMonth()
{
switch(month)
{
case 4:return day==30;
case 6:return day==30;
case 9:return day==30;
case 11:return day==30;
case 2:
{
if(isLeapYear()==1){return day==29;}
else{return day==28;}
}
default:return day==31;
}
}
int Date::isLeapYear()
{
if((year%400==0)||(year%4==0&&year%100!=0)){return 1;}
else{return 0;}
}
int Date::isLeapYear(int year)
{
if((year%400==0)||(year%4==0&&year%100!=0)){return 1;}
else{return 0;}
}
void Date::print()
{
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
Date::Date()
{
year=1900;
month=1;
day=1;
}
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
Date Date::addDays(int days)
{
Date d=*this;//Date d=d1;
for(int i=1;i<=days;++i)
{
d.addOneDay();
}
return d;
}
Date addDays(Date dd1,int days)
{
Date d=dd1;
for(int i=1;i<=days;++i)
{
d.addOneDay();
}
return d;
}
int daysOf2Date(Date d1,Date d2)
{
int days1=d1.calculateDays();
int days2=d2.calculateDays();
int days=abs(days1-days2);//abs();
return days;
}
int Date::daysOf2Date(Date d2)
{
Date d1=*this;
int days1=d1.calculateDays();
int days2=d2.calculateDays();
int days=abs(days1-days2);
return days;
}
int main()
{
Date d1(2019,5,1);
Date d2=d1.addDays(5);//this指向d1;
d2.print();
Date d5=addDays(d1,28);
Date d3(2019,5,1),d4(2019,6,2);
int days=daysOf2Date(d3,d4);
cout<<days<<endl;
return 0;
}