実験2類の定義と応用
9093 ワード
1、実験目的と要求:
クラス定義の方法を把握し,クラスタイプ,インスタンスの意味を理解し,オブジェクト向けプログラム設計におけるデータ隠蔽の概念を体得する.コンストラクション関数とコンストラクション関数の役割と実行時間を理解し、コンストラクション関数をコピーする役割を把握します.複雑なオブジェクトタイプの運用を熟練しています.
2、実験内容:
(1)時間クラスTimeを定義し、時間、分、秒からなる時間を提供することができる.時間とプリントアウト時間を変更できることが求められます.
(2)カウンタクラスを記述し,プライベートデータメンバーを定義し,2つのメンバー関数によりそれぞれプラス1とマイナス1の操作を完了させる.コンストラクション関数データメンバーを0に初期化し、出力関数はデータメンバーの値を出力します.
(3)時間クラスを設計し、以下の機能を含むことを要求する.
データ・メンバー:時間、分、秒
メンバー関数:
各種リロードコンストラクタ
こうぞうかんすう
関数で時間の各コンポーネントを個別に設定できます
全体的に時間を設定することもできます
現在の時間に1秒を加えて新しい時間を出力するtick関数があります
(4)スコアクラスを次のように定義し,各メンバ関数を実装し,主関数で2つのスコアの加減算などの演算をテストする.
class Rational
{
public:
Rational(int nn=1,int mm=1);//コンストラクタ
Rational R_add(Rational & A);//プラス
Rational R_sub(Rational & A);//マイナス
Rational R_mul(Rational & A);//乗る
Rational R_div(Rational & A);//除
void print();//簡単な点数で表示し、約点に注意
private:
void simple( );//約分
int m;//分母
int n;//分子
};
(5)クラスに整数を含むポインタ変数を定義し、コンストラクション関数にnewで10整数のメモリ空間を割り当て、コンストラクション関数にdeleteでメモリ空間を解放し、メモリ空間に値と出力を割り当てるメンバー関数を記述する.
(6)Stringクラスの完了
class String
{
public:
String(const char *str = NULL);//一般コンストラクタ
String(const String &other);//コピーコンストラクタ
~ String();//解析関数
private:
char *m_data;//文字列の保存
};
Stringの3つのメンバー関数を完了し、作成したメンバー関数をテストするメイン関数を作成します.
(7)Arrayクラスの完了
class Array
{
public:
Array();//すべての配列要素を0に初期化
int& getData(int i);//iと表記された配列要素の参照を返す
void print();//すべての配列要素の値を印刷する
void input();//すべての配列要素を入力する
private:
int m_data[10];
};
Arrayのメンバー関数を完了し、作成したメンバー関数をテストするメイン関数を作成します.
(8)円類(円心座標点類と半径からなる)を定義し、2つの円の位置関係(円間関係は交差、正接、相離を含む)を友元関数で判断する.
(9)定義クラスPoint.クラス・オブジェクトの定義を要求する場合は、次のような定義文があります.
Point p1(1,2),p2(p1);
#include
class Point
{
public:
Point(int a, int b)
{
x=a;y=b;
}
void fun (Point &p);
void fun (Point * p);
private:
intx,y;
};
void fun (Point &p)
{
x=p.x ;
y=p.y ;
cout<< "The fun(Point &p)"<}
void Point::fun (Point *p)
{
x=p->x ;
y=p->y ;
cout<<"Fun (Point *p) "<}
Void main()
{
Pointp(1,2),q(3,4) ;
p.fun(q) ;
q.fun(&q) ;
}
プログラム中のエラーを修正し,プログラム実行の結果を提示し,プログラムを分析する.
クラス定義の方法を把握し,クラスタイプ,インスタンスの意味を理解し,オブジェクト向けプログラム設計におけるデータ隠蔽の概念を体得する.コンストラクション関数とコンストラクション関数の役割と実行時間を理解し、コンストラクション関数をコピーする役割を把握します.複雑なオブジェクトタイプの運用を熟練しています.
2、実験内容:
(1)時間クラスTimeを定義し、時間、分、秒からなる時間を提供することができる.時間とプリントアウト時間を変更できることが求められます.
#include<iostream>
using namespace std;
class time
{
private:
int hour,minute,second;
public:
time(int h=0,int m=0,int s=0)
{
if(h<0||h>23||m<0||m>59||s<0||s>59)
{
cout<<" "<<endl;
}
else
{
this->hour=h;
this->minute=m;
this->second=s;
}
}
~time() {}
void setTime()
{
int h,m,s;
cout<<" (h,m,s)"<<endl;
cin>>h>>m>>s;
if(h<0||h>23||m<0||m>59||s<0||s>59)
{
cout<<" "<<endl;
return;
}
this->hour=h;
this->minute=m;
this->second=s;
}
void print()
{
cout<<hour<<" "<<minute<<" "<<second<<" "<<endl;
}
};
int main()
{
time t;
cout<<" :"<<endl;
t.print();
t.setTime();
cout<<" :"<<endl;
t.print();
}
(2)カウンタクラスを記述し,プライベートデータメンバーを定義し,2つのメンバー関数によりそれぞれプラス1とマイナス1の操作を完了させる.コンストラクション関数データメンバーを0に初期化し、出力関数はデータメンバーの値を出力します.
(3)時間クラスを設計し、以下の機能を含むことを要求する.
データ・メンバー:時間、分、秒
メンバー関数:
各種リロードコンストラクタ
こうぞうかんすう
関数で時間の各コンポーネントを個別に設定できます
全体的に時間を設定することもできます
現在の時間に1秒を加えて新しい時間を出力するtick関数があります
(4)スコアクラスを次のように定義し,各メンバ関数を実装し,主関数で2つのスコアの加減算などの演算をテストする.
class Rational
{
public:
Rational(int nn=1,int mm=1);//コンストラクタ
Rational R_add(Rational & A);//プラス
Rational R_sub(Rational & A);//マイナス
Rational R_mul(Rational & A);//乗る
Rational R_div(Rational & A);//除
void print();//簡単な点数で表示し、約点に注意
private:
void simple( );//約分
int m;//分母
int n;//分子
};
#include<iostream>
#include<stdlib.h>
using namespace std;
class Rational
{
public:
Rational(int nn=1,int mm=1); //
Rational R_add(Rational & A); //
Rational R_sub(Rational & A); //
Rational R_mul(Rational & A); //
Rational R_div(Rational & A); //
void print(); // ,
private:
void simple(); //
int m; //
int n; //
};
Rational::Rational(int nn,int mm)
{
if(mm==0)
{
cout<<" 0"<<endl;
exit(0);
}
this->m=mm;
this->n=nn;
}
void Rational::simple()
{
int a=m,b=n,r;
while(r=a%b)//
{
a=b;
b=r;
}
m=m/b;
n=n/b;
}
Rational Rational::R_add(Rational & A)
{
this->n=n*A.m+m*A.n;
this->m=m*A.m;
return *this;
}
Rational Rational::R_sub(Rational & A)
{
this->n=n*A.m-m*A.n;
this->m=m*A.m;
return *this;
}
Rational Rational::R_mul(Rational & A)
{
this->n=n*A.n;
this->m=m*A.m;
return *this;
}
Rational Rational::R_div(Rational & A)
{
this->n=n*A.m;
this->m=m*A.n;
return *this;
}
void Rational::print()
{
simple();
cout<<n<<"/"<<m<<endl;
}
int main()
{
Rational r(3,9);
r.print();
Rational p(2,4);
p.print();
//
r.R_add(p);
r.print();
r.R_sub(p);
r.print();
r.R_mul(p);
r.print();
r.R_div(p);
r.print();
}
(5)クラスに整数を含むポインタ変数を定義し、コンストラクション関数にnewで10整数のメモリ空間を割り当て、コンストラクション関数にdeleteでメモリ空間を解放し、メモリ空間に値と出力を割り当てるメンバー関数を記述する.
#include<iostream>
using namespace std;
class Integer
{
private:
int *p;
int size;
public:
Integer(int size=10)
{
this->size=size;
p=new int[size];// new 10
}
~Integer()// delete
{
delete []p;
}
void set()
{
cout<<" "<<size<<" :"<<endl;
for(int i=0;i<size;i++)
cin>>p[i];
}
void print()
{
for(int i=0;i<size;i++)
{
cout<<p[i]<<" ";
}
cout<<endl;
}
};
int main()
{
Integer i;
i.set();
i.print();
return 0;
}
(6)Stringクラスの完了
class String
{
public:
String(const char *str = NULL);//一般コンストラクタ
String(const String &other);//コピーコンストラクタ
~ String();//解析関数
private:
char *m_data;//文字列の保存
};
Stringの3つのメンバー関数を完了し、作成したメンバー関数をテストするメイン関数を作成します.
#include<iostream>
#include<cstring>
using namespace std;
class String
{
public:
String(const char *str = NULL); //
String(const String &other); //
~ String(); //
void print();
private:
char *data; //
};
String::String(const char *str)
{
if(str)
{
data=new char[strlen(str)+1];
strcpy(data,str);
}
else
{
data=new char[8];
strcpy(data,"<Empty>");
}
cout<<data<<" Constructing"<<endl;
}
String::String(const String &other)
{
if(other.data)
{
data=new char[strlen(other.data)+1];
strcpy(data,other.data);
}
else
{
data=new char[8];
strcpy(data,"<Empty>");
}
cout<<data<<" Copy Constructing"<<endl;
}
String::~String()
{
cout<<data<<" Destructing"<<endl;
delete []data;
}
void String::print()
{
cout<<"data="<<data<<endl;
}
int main()
{
String s1("dut");
String s2(s1);
String s3;
s1.print();
s2.print();
s3.print();
return 0;
}
(7)Arrayクラスの完了
class Array
{
public:
Array();//すべての配列要素を0に初期化
int& getData(int i);//iと表記された配列要素の参照を返す
void print();//すべての配列要素の値を印刷する
void input();//すべての配列要素を入力する
private:
int m_data[10];
};
Arrayのメンバー関数を完了し、作成したメンバー関数をテストするメイン関数を作成します.
#include<iostream>
using namespace std;
class Array
{
public:
Array(); // 0
int& getData(int i); // i
void print(); //
void input(); //
private:
int data[10];
};
Array::Array()
{
for(int i=0; i<10; i++)
data[i]=0;
}
int& Array::getData(int i)
{
return data[i];
}
void Array::input()
{
cout<<"Please input 10 numbers"<<endl;
for(int i=0; i<10; i++)
cin>>data[i];
}
void Array::print()
{
for(int i=0; i<10; i++)
cout<<data[i]<<" ";
cout<<endl;
}
int main()
{
Array a;
a.print();
a.input();
cout<<a.getData(5)<<endl;
a.print();
return 0;
}
(8)円類(円心座標点類と半径からなる)を定義し、2つの円の位置関係(円間関係は交差、正接、相離を含む)を友元関数で判断する.
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
private:
double x,y;
friend class Circle;
public:
Point():x(0),y(0) {}
Point(double xx,double yy):x(xx),y(yy) {}
};
class Circle
{
private:
Point center;
double radius;
public:
Circle():radius(0) {}
Circle(double a,double b,double r):center(a,b),radius(r) {}
void relation(Circle &c1)
{
double r=sqrt((c1.center.x-center.x)*(c1.center.x-center.x)+(c1.center.y-center.y)*(c1.center.y-center.y))-(c1.radius+radius);
if(r>0)
cout<<" "<<endl;
else if(r==0)
cout<<" "<<endl;
else
cout<<" "<<endl;
}
};
int main()
{
Circle c1(10,10,2),c2(10,14,2);
c1.relation(c2);
return 0;
}
(9)定義クラスPoint.クラス・オブジェクトの定義を要求する場合は、次のような定義文があります.
Point p1(1,2),p2(p1);
#include
class Point
{
public:
Point(int a, int b)
{
x=a;y=b;
}
void fun (Point &p);
void fun (Point * p);
private:
intx,y;
};
void fun (Point &p)
{
x=p.x ;
y=p.y ;
cout<< "The fun(Point &p)"<
void Point::fun (Point *p)
{
x=p->x ;
y=p->y ;
cout<<"Fun (Point *p) "<
Void main()
{
Pointp(1,2),q(3,4) ;
p.fun(q) ;
q.fun(&q) ;
}
プログラム中のエラーを修正し,プログラム実行の結果を提示し,プログラムを分析する.