/* 程式檔名:Test_C9-39-1.cpp */
/* 程式目的:設計一個Car類別,其資料成員包括油箱目前油量及車重,並設計必要的建構函式,建構函式需檢查參數中的目前油量及車重是否超過指定的上限值*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150127 */
#include<iostream>
using namespace std;
class Car{
public:
Car(double,double);
void show()
{
cout << "目前油量:" << gas << "目前車重:" << carweight << endl;
}
private:
double gas;
double carweight;
};
Car::Car(double g,double w)
{
if(g > 100)
{
g=100;
cout << "油箱超過重量,自動重設100公升" << endl;
}
gas=g;
if(w > 3000)
{
w=3000;
cout << "車重超過重量,自動重設3000公斤" << endl;
}
carweight=w;
}
int main()
{
Car car1(80,2500);
car1.show();
Car car2(120,2500);
car2.show();
Car car3(50,3200);
car3.show();
system("PAUSE");
return 0;
}
/* 程式檔名:Test_C9-39-2.cpp */
/* 程式目的:續上題,請在類別加入一靜態資料成員記錄目前物件總數,並在建構函式及解建構函式中維護正確的物件總數值*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150127 */
#include<iostream>
using namespace std;
class Car{
public:
Car(double,double);
~Car();
void show()
{
cout << "目前油量:" << gas << "目前車重:" << carweight << endl;
}
int howmanycar()
{
return counter;
}
private:
double gas;
double carweight;
static int counter;
};
int Car::counter=0;
Car::Car(double g,double w)
{
if(g > 100)
{
g=100;
cout << "油箱超過重量,自動重設100公升" << endl;
}
gas=g;
if(w > 3000)
{
w=3000;
cout << "車重超過重量,自動重設3000公斤" << endl;
}
carweight=w;
counter++;
}
Car::~Car()
{
counter--;
}
int main()
{
Car car(80,2500);
cout << "現在有 " << car.howmanycar() << " 輛車" << endl;
Car *car2 = new Car (120,2500);
cout << "現在有 " << car.howmanycar() << " 輛車" << endl;
Car *car3 = new Car (50,3200);
cout << "現在有 " << car.howmanycar() << " 輛車" << endl;
delete car2;
delete car3;
cout << "刪除 car2 car3" << endl;
cout << "現在有 " << car.howmanycar() << " 輛車" << endl;
system("PAUSE");
return 0;
}
/* 程式檔名:Test_C9-39-3.cpp */
/* 程式目的:包含一個類別Dates,並在建構函式中初始化一個包含有7個元素的字串陣列,各個元素對應到星期一到星期日的英文縮寫,並提供askDATe(),傳入1~7傳回對應英文縮寫*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150127 */
#include<iostream>
using namespace std;
class Dates{
public:
Dates();
char* askDate(int);
private:
char* name[7];
int step;
};
Dates::Dates()
{
name[0] = "Mon";
name[1] = "Tue";
name[2] = "Wed";
name[3] = "Thu";
name[4] = "Fri";
name[5] = "Sat";
name[6] = "Sun";
}
char* Dates::askDate(int i)
{
if (i>7)
i=7;
else if (i<1)
i=1;
return name[i-1];
}
int main()
{
Dates date;
cout << "星期一到星期天的英文縮寫是:\n";
for(int i =1;i<=7;i++)
cout << date.askDate(i) << '\t';
cout << endl;
system("PAUSE");
return 0;
}
==============================================
/* 程式檔名:Test_C9-39-4.cpp */
/* 程式目的:承上題,再加入一個方法toChinese(),可用星期的英文縮寫為參數呼叫之,函式會傳回對應的中文星期名稱*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150306 */
#include<cstring>
#include<iostream>
using namespace std;
class Dates{
public:
Dates();
char *askDate(int);
char *toChinese(char *);
private:
char* name[7];
int step;
};
Dates::Dates()
{
name[0] = "Mon";
name[1] = "Tue";
name[2] = "Wed";
name[3] = "Thu";
name[4] = "Fri";
name[5] = "Sat";
name[6] = "Sun";
}
char* Dates::askDate(int i)
{
if (i>7)
i=7;
else if (i<1)
i=1;
return name[i-1];
}
char* Dates::toChinese(char *chname)
{
static char cname[][7] = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
for (int i=0;i<7;i++)
{
if(strcmp(chname,name[i])==0)
return cname[i];
}
}
int main()
{
Dates date;
for(int i =1;i<=7;i++)
cout << date.askDate(i) << '\t' << date.toChinese(date.askDate(i)) << '\n';
system("PAUSE");
return 0;
}
==============================================
/* 程式檔名:Test_C9-39-5.cpp */
/* 程式目的:修改Clock 類別,增加建構函式可設定目前的時間,另有一setAlarm()成員函式可設定鬧鐘時間
/* 程式設計:蘇彥儒*/
/* 完成日期:20150306 */
#include<iostream>
using namespace std;
class Time{
public:
Time(int h=12, int m=0, int s=0)
{
hour=h; min=m; sec=s;
hour = (h>=0 && h<=23) ? h:12;
min = (m>=0 && m<=59) ? m:0;
sec = (s>=0 && s<=59) ? s:0;
}
void set(int h,int m,int s)
{
hour=h;min=m;sec=s;
}
void show()
{
cout << hour << "點" << min << "分" << sec << "秒" << endl;
}
private:
int hour, min, sec;
};
class Clock{
public:
Clock(int h, int m, int s=0): clock_time(h,m,s) {};
void setAlarm(int h,int m,int s)
{
alarm_time.set(h,m,s);
}
void show();
private:
Time clock_time;
Time alarm_time;
};
void Clock::show()
{
cout << "目前的時間:" << endl;
clock_time.show();
cout << "鬧鐘的時間:" << endl;
alarm_time.show();
}
int main()
{
Clock old_time(10,1,12);
old_time.setAlarm(8,11,10);
old_time.show();
system("PAUSE");
return 0;
}
/* 程式檔名:Test_C9-39-6.cpp */
/* 程式目的:請撰寫一個代表矩形類別Rectangle,並提供多種建構方法,例如可以指定左上角頂點座標及長寬值。
/* 程式設計:蘇彥儒*/
/* 完成日期:20150306 */
#include<iostream>
using namespace std;
class Rectangle{
public:
Rectangle(int,int,double,double);
Rectangle(double,double,double,double);
void show()
{
cout << "x1=" << x1 << endl;
cout << "x2=" << x2 << endl;
cout << "y1=" << y1 << endl;
cout << "y2=" << y2 << endl;
}
void showwh()
{
cout << "weigh=" << weight << endl;
cout << "hight=" << hight << endl;
cout << "x1=" << x1 << endl;
cout << "y1=" << y1 << endl;
}
private:
double x1,x2;
double y1,y2;
int weight,hight;
};
Rectangle::Rectangle(double x1,double x2,double y1,double y2)
{
this->x1=x1;
this->x2=x2;
this->y1=y1;
this->y2=y2;
}
Rectangle::Rectangle(int wt,int ht,double x1,double y1)
{
this->weight=wt;
this->hight=ht;
this->x1=x1;
this->y1=y1;
}
int main()
{
Rectangle c1(5,6,4.3,3.5);
c1.showwh();
cout << endl;
Rectangle c2(10.1,10.2,30.6,30.7);
c2.show();
system("PAUSE");
return 0;
}
/* 程式檔名:Test_C9-39-7.cpp */
/* 程式目的:請承上題,新增一個成員函式,傳回bool型別,表示該矩形是否為正方形。
/* 程式設計:蘇彥儒*/
/* 完成日期:20150306 */
#include<iostream>
using namespace std;
class Rectangle{
public:
Rectangle(int,int,double,double);
Rectangle(double,double,double,double);
bool checkRe(void);
void show()
{
cout << "x1=" << x1 << endl;
cout << "x2=" << x2 << endl;
cout << "y1=" << y1 << endl;
cout << "y2=" << y2 << endl;
}
void showwh()
{
cout << "weigh=" << weight << endl;
cout << "hight=" << hight << endl;
cout << "x1=" << x1 << endl;
cout << "y1=" << y1 << endl;
}
private:
double x1,x2;
double y1,y2;
int weight,hight;
};
Rectangle::Rectangle(double x1,double x2,double y1,double y2)
{
this->x1=x1;
this->x2=x2;
this->y1=y1;
this->y2=y2;
}
Rectangle::Rectangle(int wt,int ht,double x1,double y1)
{
this->weight=wt;
this->hight=ht;
this->x1=x1;
this->y1=y1;
}
bool Rectangle::checkRe()
{
double x = (x1 > x2) ? (x1 - x2) : (x2 - x1);
double y = (y1 > y2) ? (y1 - y2) : (y2 - y1);
if(x == y)
{
return true;
}
else
{
return false;
}
}
int main()
{
Rectangle c1(5,6,4.3,3.5);
c1.showwh();
cout << endl;
Rectangle c2(4.0,2.0,4.0,2.0);
c2.show();
cout << "是否為正方形:";
if(c2.checkRe())
{
cout << "是" << endl;
}
else
{
cout << "否" << endl;
}
system("PAUSE");
return 0;
}
/* 程式檔名:Test_C9-39-8.cpp */
/* 程式目的:圓形類別增加一個建構函式,可用Rectangle物件為參數*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150306 */
#include<iostream>
using namespace std;
class Rectangle{
public:
Rectangle(int,int,double,double);
Rectangle(double,double,double,double);
bool checkRe(void);
void show()
{
cout << "x1=" << x1 << endl;
cout << "x2=" << x2 << endl;
cout << "y1=" << y1 << endl;
cout << "y2=" << y2 << endl;
}
private:
double x1,x2;
double y1,y2;
int weight,hight;
};
bool Rectangle::checkRe()
{
double x = (x1 > x2) ? (x1 - x2) : (x2 - x1);
double y = (y1 > y2) ? (y1 - y2) : (y2 - y1);
if(x == y)
{
return true;
}
else
{
return false;
}
}
Rectangle::Rectangle(double x1,double x2,double y1,double y2)
{
this->x1=x1;
this->x2=x2;
this->y1=y1;
this->y2=y2;
}
class Circle{
public:
Circle(double,double,double);
Circle(double,double,double,double);
Circle(Rectangle);
double area()
{
return 3.1415926 * r * r;
}
double circum()
{
return 3.1415926 * 2 * r;
}
private:
double x,y;
double r;
};
Circle::Circle(double x0,double y0,double r0 =1)
{
x=x0;
y=y0;
r=r0;
}
Circle::Circle(double x0,double y0,double x1,double y1)
{
double w = min ((x1>x0)?(x1-x0):(x0-x1) , (y1>y0)?(y1-y0):(y0-y1));
r = w/2;
x = x0 + r;
y = y0 + r;
}
Circle::Circle(Rectangle rect1)
{
rect1.show();
cout << "是否為正方形?";
if(rect1.checkRe())
cout << " 是!" << endl;
else
cout << " 否!" << endl;
}
int main()
{
Circle c1(3,5),c2(10,10,30,30);
cout << "c1 的面積為 " << c1.area() << endl;
cout << "c2 的圓周長為 " << c2.circum() << endl;
Rectangle add1(2.0, 2.0, 5.0, 5.0);
Circle c3(add1);
system("PAUSE");
return 0;
}
/* 程式檔名:Test_C9-39-9.cpp */
/* 程式目的:請設計一學生類別,內函學號、學生姓名,數學及英文成績,並設計適當的建構函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150309 */
#include<iostream>
using namespace std;
class student{
public:
student(char*,double,double);
void show();
private:
int ID;
static int counter;
char *name;
double math;
double eng;
};
int student::counter=0;
student::student(char* name,double math,double eng)
{
counter++;
ID=99900000 + counter;
this->name=name;
this->math=math;
this->eng=eng;
}
void student::show()
{
cout << "學生ID:" << ID << "\t名子:" << name << "\t數學成績:" <<math << "\t英文成績:" << eng << endl;
}
int main()
{
student* stu[2]={new student("陳小新",80.8,85.2),new student("王曉明",50.8,90.2)};
for(int i =0;i<2;i++)
stu[i]->show();
system("PAUSE");
return 0;
}
/* 程式檔名:Test_C9-39-10.cpp */
/* 程式目的:請設計一學生類別,內函學號、學生姓名,數學及英文成績,並設計適當的建構函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150309 */
#include<iostream>
using namespace std;
class student{
public:
student(char*,double,double);
void show();
student(student &);
private:
int ID;
static int counter;
char *name;
double math;
double eng;
};
int student::counter=0;
student::student(student& st)
{
counter++;
name=st.name;
ID=99900000 + counter ;
math=0;
eng=0;
}
student::student(char* name,double math,double eng)
{
counter++;
ID=99900000 + counter;
this->name=name;
this->math=math;
this->eng=eng;
}
void student::show()
{
cout << "學生ID:" << ID << "\t名子:" << name << "\t數學成績:" <<math << "\t英文成績:" << eng << endl;
}
int main()
{
student *stu[2]={new student("陳小新",80.8,85.2),new student("王曉明",50.8,90.2)};
student stua=*stu[1];
for(int i =0;i<2;i++)
stu[i]->show();
stua.show();
system("PAUSE");
return 0;
}
==============================================