就從第四章類與對象開始吧,前面的章節和c差不多。
1.點類定義和使用
【問題描述】
定義一個點類,該類包含整形座標x,y以及用於設置座標值的函數,名爲setxy()參數自行確定,以及用於顯示座標的函數displayxy()參數自行設置。合理編寫主函數,能夠實現(3,4)以及(5,6)固定兩個點對象的參數設置,以及信息輸出
【輸入形式】
無數據輸入,請一定使用類的定義以及對象的創建的相關知識
【輸出形式】
輸出兩個固定點的相關信息
【樣例輸入】
【樣例輸出】
The first point is:(3,4)
The second point is:(5,6)
#include<iostream>
using namespace std;
class Point {
public:
Point(int a,int b)
{ x=a;
y=b;
}
void setxy(int a,int b)
{
x=a;
y=b;
}
void display()
{
cout<<"The first point is"<<":"<<"("<<x<<","<<y<<")"<<endl;
//cout<<"The second point is"<<":"<<"("<<5<<","<<6<<")"<<endl;
}
private:
int x,y;
};
int main()
{
//int a,b,c,d;
Point s1(3,4);
Point s2(5,6);
//s.setxy(3,4);
//s.display();
//s.setxy(5,6);
s1.display();
s2.display();
return 0;
}
2.三角形類
【問題描述】
定義一個描述三角形的類Tri,其具體要求爲:
(1)私有數據成員爲三角形的三邊
(2)公有成員函數 構造函數:用以初始化指定的三角形對象;求三角形的邊長的成員函數;求三角形面積的成員函數;輸出三角形各種參數的成員函數即用以輸出三角形對象的邊長、周長和麪積。
【輸入形式】
輸入三角形的三邊長 【輸出形式】若構成三角形,輸出三角形的邊長、周長及面積,否則輸出“不構成三角形!”
【樣例輸入1】
3 4 5
【樣例輸出1】
三角形的邊長:3 4 5 三角形的周長:12 三角形的面積:6
【樣例輸入1】
1 2 3
【樣例輸出1】
不構成三角形!
#include<iostream>
#include<math.h>
using namespace std;
class Tri
{
double a,b,c; //定義三角形的三邊
public:
Tri(double x, double y, double z) //構造函數,初始化三邊
{ a=x; b=y;c=z; }
double Peri() //返回三角形的周長
{ return (a+b+c); }
double Area() //返回三角形的面積
{ double s=Peri()/2;
double area=sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
void Show() //輸出三角形的參數
{ cout<<"三角形的邊長:"<<a<<' '<<b<<' '<<c<<endl;
cout<<"三角形的周長:"<<Peri()<<endl;
cout<<"三角形的面積:"<<Area()<<endl<<endl;
}
};//三角形類的定義結束,定義了三角形的各種屬性和可實施的操作
int main()
{ double x,y,z;
cin>>x>>y>>z;
Tri tri(x,y,z);
if(x+y>z&&y+z>x&&x+z>y)
tri.Show(); //輸出這兩個三角形的參數
else
cout<<"不構成三角形!"<<endl;
}
3.設計一個Time類
【問題描述】
定義了一個以hours, minutes和seconds作爲數據成員的Time類。設計了成員函數將兩個Time對象相加(即時間相加),並進行相應的檢查,查看增加的分鐘數及秒數是否大於59。如果秒數大於59,則分鐘數向前遞增1。類似地,如果分鐘數大於59,則小時數向前增1。
【輸入形式】
輸入兩個由時、分、秒構成的時間。
【輸出形式】
輸出輸入的兩個時間相加後的時間
【樣例輸入】
2 34 45 1 47 56
【樣例輸出】
the result is:4:22:41
【樣例輸入】
2 67 100 1 56 200
【樣例輸出】
the result is:5:8:0
#include<iostream>
using namespace std;
class Time
{
private:
int hours, minutes, seconds;
public:
void get_time()
{
cin>>hours>>minutes>>seconds;
}
void display_time()
{
cout<<hours<<':'<<minutes<<':'<<seconds<<endl;
}
void add_time(Time & t1, Time & t2)
{ int tmp;
hours=t1.hours+t2.hours;
minutes=t1.minutes+t2.minutes;
seconds=t1.seconds+t2.seconds;
if(seconds>=60)
{ tmp=seconds/60;
seconds-=tmp*60;
minutes=minutes+tmp;
}
if(minutes>=60)
{ tmp=minutes/60;
minutes-=tmp*60;
hours=hours+tmp;
}
}
};
int main()
{
Time one, two, three;
//cout<<"Enter the first time(hours minutes seconds):";
one.get_time();
//cout<<"Enter the second time(hours minutes seconds):";
two.get_time();
three.add_time(one,two);
cout<<"the result is:";
three.display_time();
}
4.datatype(數據類型)類
【問題描述】
聲明一個datatype(數據類型)類,該類能夠根據用戶的輸入,確定輸入的數據類型,能處理包含字符型、整形、浮點型3種類型的數據,並給出合理的輸出。提示:需要進行構造函數的重載
【輸入形式】
給用戶選擇,當輸入1時,選擇輸入整型;輸入2時,輸入字符型;選擇3時,輸入浮點型。不考慮其他錯誤情況
【輸出形式】
輸入該數據以及該數據的類型
【樣例輸入1】
2c
【樣例輸出1】
character:c
【樣例輸入2】
112
【樣例輸出2】
int:12
【樣例輸入3】
31.44F
【樣例輸出3】
float:1.44
#include <iostream>
using namespace std;
class DataType{
enum
{
character,
integer,
floating_point
} vartype;
union
{
char c;
int i;
float f;
};
public:
DataType(char ch)
{
vartype = character;
c = ch;
}
DataType(int ii)
{
vartype = integer;
i = ii;
}
DataType(float ff)
{
vartype = floating_point;
f = ff;
}
void print();
};
void DataType::print()
{
switch (vartype)
{
case character:
cout << "字符型: " << c << endl;
break;
case integer:
cout << "整型: " << i << endl;
break;
case floating_point:
cout << "浮點型: " << f << endl;
break;
}
}
int main()
{ DataType a('c'), b(12), c(1.44F);
a.print();
b.print();
c.print();
return 0;
}
5.複數類Complex
【問題描述】
定義一個複數類,使得下面的代碼能夠工作:
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
【輸入形式】
無
【輸出形式】
c1=3 + 5i
c2=4.5 + 0i
c1+c2=7.5 + 5i
#include <iostream>
using namespace std;
class Complex {
public:
Complex(double r, double i) :real(r), image(i) { }
Complex(double r) :real(r), image(0) {}
void show();
void add(Complex c2);
private:
double real;
double image;
};
void Complex::add(Complex c2) {
real += c2.real;
image += c2.image;
}
void Complex::show() {
cout << real << "+";
cout << image << "i";
cout << endl;
}
int main() {
Complex c1(3, 5);
Complex c2=4.5;
cout<<"c1=";
c1.show();
cout<<"c2=";
c2.show();
c1.add(c2);
cout<<"c1+c2=";
c1.show();
return 0;
}
6.計算由圓和正方形構成的陰影部分的面積
【問題描述】
定義一個圓形類,屬性有半徑和相應的成員函數。然後定義一個正方形類,屬性有邊長和相應的成員函數。再編寫一個如下圖所示的組合類,由一個正方型和一個圓形組成,要求該組合類能求出陰影部分面積和周長。
【輸入形式】
無
【輸出形式】
自定義圖形的面積49.2656 自定義圖形的周長29.1328 自定義圖形的面積109.098 自定義圖形的周長45.6992
#include <iostream>
using namespace std;
const double PI=3.1416;
/**********Program**********/
class Box //正方形類
{
private:
int A;
public:
Box( ){ } //默認構造函數
Box(int x){ A=x;} //構造函數
void set(double a){ A=a;} //設置邊長
double S(){ return A*A; } //求面積
double BL(){ return 4*A; } //求周長
};
class circle //圓類
{
private:
int B;
public:
circle( ){ }; //默認構造函數
circle(int x){B=x;} //構造函數
void set(double b){ B=b;} //設置半徑
double CL(){ return 2*PI*B; } //求周長
double S(){ return PI*B*B;} //求面積
};
class NewStyle //組合類
{
private:
circle A;
Box B;
public:
NewStyle( ) {} //默認構造函數
NewStyle(circle x,Box y):A(x),B(y){} //構造函數
void set(circle x,Box y){ A=x;B=y;} //設置組合圖形
double S(){ return A.S()-B.S();} //求面積
double L(){ return A.CL()+B.BL();}; //求周長
} ;
/********** End **********/
int main()
{
circle A(4); //圓的半徑爲4
Box B(1); //正方形的邊長爲1
NewStyle C(A,B);
cout<<"自定義圖形的面積"<<C.S()<<endl;
cout<<"自定義圖形的周長"<<C.L()<<endl;
A.set(6); //圓的半徑變爲6
B.set(2); //正方形的邊長變爲2
C.set(A,B);
cout<<"自定義圖形的面積"<<C.S()<<endl;
cout<<"自定義圖形的周長"<<C.L()<<endl;
return 0;
}
7.CPU類
【問題描述】
聲明一個CPU類。包含等級(rank)、頻率(frequency)、電壓(voltage)等屬性,有兩個公有成員函數run、stop,分別提示“CPU開始運行!”和“CPU停止運行!”。其中,rank爲枚舉類型CPU_Rank,聲明爲enum CPU_Rank{ P1=1, P2, P3, P4, P5, P6, P7 }; frequency爲單位是MHz的整型數,voltage爲浮點型的電壓值。用2個CPU對象進行測試觀察構造函數和析構函數的調用順序。
【輸入形式】
輸入CPU的等級,1表示P1,3代表P3
【輸出形式】
CPU對象的相關信息:構造函數、析構函數的調用情況,CPU對象的運行狀況及CPU的等級
【樣例輸入1】
2 5
【樣例輸出1】
構造了一個CPU!
構造了一個CPU!
CPU開始運行!
等級爲:2
CPU停止運行!
CPU開始運行!
等級爲:5
CPU停止運行!
析構了一個CPU!
析構了一個CPU!
【樣例輸入2】
1 7
【樣例輸出2】
構造了一個CPU!
構造了一個CPU!
CPU開始運行!
等級爲:1
CPU停止運行!
CPU開始運行!
等級爲:7
CPU停止運行!
析構了一個CPU!
析構了一個CPU!
#include <iostream>
using namespace std;
enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
private:
CPU_Rank rank;
int frequency;
float voltage;
public:
CPU (CPU_Rank r, int f, float v)
{
rank = r;
frequency = f;
voltage = v;
cout << "構造了一個CPU!" << endl;
}
~CPU () { cout << "析構了一個CPU!" << endl; }
CPU_Rank GetRank() const { return rank; }
int GetFrequency() const { return frequency; }
float GetVoltage() const { return voltage; }
void SetRank(CPU_Rank r) { rank = r; }
void SetFrequency(int f) { frequency = f; }
void SetVoltage(float v) { voltage = v; }
void Run() {cout << "CPU開始運行!" << "\n等級爲:"<<rank<<endl; }
void Stop() {cout << "CPU停止運行!" << endl; }
};
int main()
{
int r1,r2;
cin>>r1>>r2;
CPU a((CPU_Rank)r1,300,2.8);
CPU b((CPU_Rank)r2,800,8.8);
a.Run();
a.Stop();
b.Run();
b.Stop();
}
持續更新中