C ++クラスとオブジェクトは、C言語での第一の特徴言語的違いがあり、C ++言語である、白、新規のために不可欠です。
:1.
実験1:
円クラスサークルのデザイン、クラス・データ・メンバーは、中心座標(X、Y)を有し、半径rは、さらに以下の機能を有する:
(1)中心を初期座標および半径
(2)及び表示中心座標半径
(3)円の面積を計算
クラス宣言テストオブジェクトで定義された主な機能には、。
#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;
}
実験2:
直方体クラスのボックスを設計します。このクラスは、データ・メンバを有する長さ(長さ)、幅(幅)、高(H)を含みます。機能次のメンバ関数を必要とする:
(1)直方体の幅と高、長い初期化する;
(2)直方体の体積を計算するステップと
、長さの(3)ボックスの幅と高さ。
主な機能には、定義されたテストオブジェクトは、クラスを宣言しました。
#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.構築とオブジェクトとオブジェクトポインタのデストラクタ+アレイ:
移動物体3は、リリースおよび割り当てを確立する静的メンバ関数の+をコピー:
実験3:
コンストラクタとケースオブジェクトのデストラクタ影響解析を呼び出します。
#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.機能とメタ友人のクラス:
実験4:
文の複数の複雑なクラスは、2人のデータ・メンバー実数実数、虚数部IMAGがあります。このクラスは、以下の機能を有する:
(1)コンストラクタは、実数部と虚数部を初期化する;
(2)2つの錯体の添加は、操作が(複合体)機能を追加実装し、クラスのメンバ関数を使用して実装;
(3)2を実装します複素減算サブ(複合体、錯体)機能、UF-員機能の実装、
メイン関数、クラス宣言テストオブジェクトに定義されました。
#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;
}