📄 memberofclass.cpp
字号:
#include <iostream.h>
//【例10.1】 定义一个学生类。在类体内定义一个成员函数,在类外定义一个成员函数。
class CStudent
{
int id;
int age;
public:
void Set(int,int); //对成员函数作引用说明
void Print()
{cout<<"ID="<<id<<'\t'<<"Age="<<age<<endl;}
};
void CStudent::Set(int a,int b)
{
id=a;
age=b;
}
void main()
{
CStudent stu;
stu.Set(1,20);
stu.Print();
}
//【例10.2】 设计一个三角形的类,求出三角形的面积。
#include<math.h>
class CTria
{
float x,y,z;
public:
void SetData(float a,float b,float c)
{
if(a+b>c&&a+c>b&&b+c>a)
{x=a;y=b;z=c;}
else x=y=z=0;
}
float Area()
{
float t=(x+y+z)/2;
return sqrt(t*(t-x)*(t-y)*(t-z));
}
};
void main10_2()
{
CTria a1;
a1.SetData(3,4,5); //A
cout<<"三角形面积="<<a1.Area()<<'\n'; //B
}
//【例10.3】成员函数的重载:处理一个数组构成的线性表,动态产生线性表,并输出线性表中的数据。
class ListClass
{
int *List; //指向线性表的指针
unsigned nMax; //线性表中最大长度
unsigned nElem; //表中当前元素的个数
public:
void Init(int n=10) //初始化线性表,最大长度的缺省值为10
{
List=new int[n]; //动态分配存储空间
nMax=n;
nElem=0; //表中当前元素的个数为0
}
int Elem(int);
int &Elem(unsigned n){ return List[n]; } //返回线性表中第n个元素的引用
unsigned Elem(void){ return nElem;} //获取当前元素的个数
unsigned Max(void){ return nMax; } //获取线性表的长度
void Print(void);
int GetElem(int i) //获取线性表中第i个元素的值
{
if((i>=0)&&(i<=nElem))return List[i];
else return 0;
}
void Destroy(void) { delete [nMax]List;} //释放线性表占用的存储空间
};
int ListClass::Elem(int elem) //在线性表的末尾增加一个元素,返回当前元素的个数
{
if(nElem<nMax){ //线性表未满,向末尾加一个元素
List[nElem++]=elem;
return nElem;
}
else{ //线性表已满,重新申请一个新的线性表
int *list;
list=new int[nMax+1];
for(int i=0;i<nElem;i++)list[i]=List[i]; //将线性表的数据保存在list中
delete [nMax]List; //将原来的线性表所占用的空间释放
nMax++; //将线性表的最大长度加1
List=list;
List[nElem++]=elem;
return nElem;
}
}
void ListClass::Print(void) //输出线性表的所有元素
{
for(int i=0;i<nElem;i++)cout<<List[i]<<'\t';
cout<<'\n';
}
void main10_3(void)
{
ListClass list,list1;
list.Init(10);
list1.Init(20);
for(int i=0;i<10;i++)
list1.Elem(i);
cout<<"线性表list的元素的个数为:"<<list.Elem()<<'\n';
cout<<"线性表list的长度为:"<<list.Max()<<'\n';
cout<<"线性表list1的元素的个数为:"<<list1.Elem()<<'\n';
cout<<"线性表lsit1长度为:"<<list1.Max()<<'\n';
list1.Print();
list1.Elem(3u)=100;
cout<<"现在线性表list1中的第三个值为:"<<list1.Elem(3u)<<'\n';
list1.Elem(20);
list1.Elem(200);
cout<<"现在线性表list1中元素的个数为:"<<list1.Elem()<<'\n';
list1.Print();
cout<<"线性表list1中的最后一个元素为:"
<<list1.GetElem(list1.Elem()-1)<<'\n';
list.Destroy();
list1.Destroy();
}
//【例10.4】成员函数与非成员函数之间的函数名可以相同。
class CAdd
{
public:
int Add(int a,int b) //A
{ return a+b;}
double Add(double x,double y) //B
{return x+y;}
};
int Add(int a,int b) //C
{ return a+b; }
void main10_4()
{
CAdd c;
cout <<"5+3="<<c.Add(5,3)<<endl; //调用A行定义的函数
cout <<"3.5+6.1="<<c.Add(3.5,6.1)<<endl; //调用B行定义的函数
cout<<"5+10="<<Add(5,10)<<endl; //调用C行定义的函数
}
//【例10.5】 构造函数的初始化作用
class CPoint
{
int x,y;
public:
CPoint(int vx,int vy); //声明构造函数
void OffSet(int ax,int ay);
void Print(){
cout<<"横坐标为:"<<x<<'\n';
cout<<"纵坐标为:"<<y<<'\n';
}
};
CPoint::CPoint(int vx,int vy){ //定义构造函数
x=vx;
y=vy;
cout<<"调用构造函数CPoint(int,int)!\n";
}
void CPoint::OffSet(int ax,int ay)
{
x=x+ax;
y=y+ay;
}
void main10_5_1()
{
CPoint pt1(20,30); //A
pt1.Print();
pt1.OffSet(10,10);
pt1.Print();
}
//创建对象指针和对象引用时构造函数的调用情况
void main10_5_2()
{
CPoint *pt1; //B
cout<<"标志1...\n";
pt1=new CPoint(30,40); //C
CPoint pt2(20,30);
cout<<"标志2...\n";
CPoint &pt3=pt2; //D
}
//【例10.6】构造函数的重载
class Cylinder
{
public:
Cylinder(){}
Cylinder(double r,double h);
void setcylinder(double r,double h);
double getradius(){ return radius; }
double getheight(){ return height; }
double volume();
double surface_area();
private:
double radius;
double height;
};
const double PI=3.1415926;
Cylinder::Cylinder(double r,double h)
{
radius=r;
height=h;
}
void Cylinder::setcylinder(double r,double h)
{
radius=r;
height=h;
}
double Cylinder::volume()
{
double vol;
vol=PI*radius*radius*height;
return vol;
}
double Cylinder::surface_area()
{
double area;
area=2*PI*radius*height+2*PI*radius*radius;
return area;
}
void main10_6()
{
Cylinder cylinder1(7.0,12.0), cylinder2; //A
cylinder2.setcylinder(12.3,18.7);
cout<<"the radius of cylinder1 is:\t"<<cylinder1.getradius()<<endl;
cout<<"the height of cylinder1 is:\t"<<cylinder1.getheight()<<endl;
cout<<"the volume of cylinder1 is:\t"<<cylinder1.volume()<<endl;
cout<<"the surface area of cylinder1 is:\t"<<cylinder1.surface_area()<<endl;
cout<<"the radius of cylinder1 is:\t"<<cylinder2.getradius()<<endl;
cout<<"the height of cylinder1 is:\t"<<cylinder2.getheight()<<endl;
cout<<"the volume of cylinder1 is:\t"<<cylinder2.volume()<<endl;
cout<<"the surface area of cylinder1 is:\t"<<cylinder2.surface_area()<<endl;
}
//【例10.7】调用缺省构造函数
class CNum
{
int i,j;
public:
void SetData(int x, int y){ i=x, j=y; }
void Print(){cout<<i<<'\t'<<j<<endl;}
};
void main10_7()
{
CNum a1; //调用缺省构造函数
a1.Print(); //对象a1中的成员为不确定的值
a1.SetData(3,5);
a1.Print();
}
//【例10.8】具有缺省参数值的构造函数
class CPoint1
{
int x,y;
int pixel;
public:
CPoint1(){x=0,y=0,pixel=100;} //第1个构造函数
CPoint1(int vx,int vy=0); //第2个构造函数
CPoint1(int vx,int vy,int pel=100); //第3个构造函数
void Print()
{
cout<<"横坐标为:"<<x<<'\t';
cout<<"纵坐标为:"<<y<<'\t';
cout<<"象素为:"<<pixel<<'\n';
}
};
CPoint1::CPoint1(int vx,int vy)
{
x=vx;
y=vy;
pixel=0;
}
CPoint1::CPoint1(int vx,int vy,int pel)
{
x=vx;
y=vy;
pixel=pel;
}
void main10_8()
{
CPoint1 pt1; //A
CPoint1 pt2(30); //B
CPoint1 pt3(20,30,100); //C
pt1.Print();
pt2.Print();
pt3.Print();
}
//【例10.9】 只有一个参数的构造函数实现类型的自动转换
class B
{
int i;
public:
B(){cout<<"调用构造函数B()!\n";}
};
class A
{
int i;
public:
A(){cout<<"调用构造函数A()!\n";}
A(int a)
{
i=a;
cout <<"i="<<i<<'\t'<<"调用构造函数A(int)!\n";
}
A(B y,int a=10)
{
i=a;
cout <<"i="<<i<<'\t'<<"调用构造函数A(B)!\n";
}
};
void main10_9()
{
A a1(10); //A
A a2=20; //B
a2=50; //C
B b; //D
A a3=b; //E
a3=b; //F
}
//【例10.10】 用构造函数进行强制类型转换
class CSample
{
int x,y;
public:
CSample(int a,int b)
{
x=a;y=b;
cout<<"x="<<x<<'\t'<<"y="<<y<<'\t'<<"调用了构造函数!\n";
}
};
void main10_10(void)
{
CSample x1(12,105);
x1=CSample(45,80); //A
}
//【例10.11】调用自动拷贝函数
class CPoint2
{
int x,y;
public:
CPoint2(){x=0,y=0;}
CPoint2(int vx,int vy)
{ x=vx; y=vy; }
void Print(){cout<<x<<'\t'<<y<<'\n';}
};
void main10_11()
{
CPoint2 pt1(100,200);
CPoint2 pt2(pt1); //A
CPoint2 pt3=pt1; //B
pt3=pt2; //C
pt1.Print();
pt2.Print();
pt3.Print();
}
//【例10.12】利用自定义的拷贝构造函数
class CPoint3
{
int x,y;
public:
CPoint3(){x=0,y=0;}
CPoint3(int vx,int vy)
{ x=vx; y=vy; }
CPoint3(CPoint3 &pt)
{
x=pt.x;
y=pt.y;
cout<<"调用拷贝构造函数CPoint3(CPoint3 &)!\n";
}
void Print(){cout<<x<<'\t'<<y<<'\n';}
};
void main10_12()
{
CPoint3 pt1(100,200);
pt1.Print();
CPoint3 pt2(pt1); //A
pt2.Print();
CPoint3 pt3=pt1; //B
pt3.Print();
cout<<"标志...\n";
pt3=pt2; //C
}
//【例10.13】 自定义的拷贝构造函数处理"指针悬挂"问题
#include<string.h>
class CA
{
char *ps;
public:
CA(){ps=0;}
CA(char *s) //为指针ps动态分配存储空间
{
ps=new char[strlen(s)+1];
strcpy(ps,s);
}
CA(CA &s);
~CA(){if(ps) delete[]ps;} //撤消指针ps所占用的存储空间
char *GetS(){return ps;}
};
CA::CA(CA &s)
{
if(s.ps){ //如果s中的成员数据非空的话
ps=new char[strlen(s.ps)+1]; //为当前对象的成员数据动态分配存储空间
strcpy(ps,s.ps); //将s中的成员数据拷贝给当前对象成员数据
}
else ps=0;
}
void main10_13(void)
{
CA s1("China!");
CA s2(s1); //A
cout<<"s1="<<s1.GetS()<<'\t';
cout<<"s2="<<s2.GetS()<<'\n';
}
//【例10.14】调用用户自定义的析构函数
class A14
{
private:
int i;
public:
A14(int a){
i=a;
cout <<"i="<<i<<'\t'<<"调用了构造函数!\n";
}
~A14() {cout<<"i="<<i<<"调用了析构函数!"<<'\n';}
};
//A14 a(0); //A
void main10_14()
{
A14 a1(10); //B
A14 a2=20; //C
a2=50; //D
cout<<"main()函数结束!"<<endl;
}
//【例10.15】 用delete运算符撤消对象时调用析构函数
class CCircle
{
private:
int r;
public:
CCircle(int a){ r=a; }
void Print(){ cout<<"半径为:"<<'\t'<<r<<endl; }
~CCircle(){cout<<"r="<<r<<"调用了析构函数!"<<'\n';}
};
void main10_15()
{
CCircle c1(10);
c1.Print();
CCircle *c2=new CCircle(20);
c2->Print();
delete c2; //A
cout<<"结束main()函数!\n"<<endl;
}
/*【例10.16】用类实现整数栈数据类型,并用该数据类型实现字符的反序操作。
栈是用来存放数据的一种数据结构,对栈中数据的操作是通过入栈和出栈进行的,入栈是指将数据存入栈中,
而出栈是将从栈中读取数据。栈总是遵循先进后出的原则,
根据这一原则,先入栈的数据总是放在栈的下放,而出栈则弹出栈的最顶端的数据。
本例用类实现简单的整数栈数据类型,并给出它的应用。*/
class CStack //栈类
{
int *top; //栈底指针,指向栈的底,即最下面的数据的前一个字节
int *bottom; //栈顶指针,指向栈的顶,即最上面的数据的后一个字节
public:
CStack() //构造函数,将栈底指针和栈顶指针同时指向一个
{ top=bottom=new int[100]; } //100字节的内存空间
void Push(int c) //入栈函数
{
if((top-bottom)<100) //当栈未满时,将c放入栈的最上面,并将顶指针
*top++=c; //向上移动一个字节
}
int Pop() //出栈函数
{ if(--top>=bottom) //如果栈非空,则返回最上面的数据
return *top;
}
~CStack() //析构函数,由于bottom和top指向同一个存储域,
{ delete []bottom; } //故不需要delete top
};
char * ReverseName(char *name) //字符串反序函数
{
CStack s;
char *reverse;
for(int i=0;i<strlen(name);i++) //将字符串的每一个字符的ACII码入栈
s.Push(name[i]);
reverse=new char[strlen(name)+1];
for(i=0;i<strlen(name);i++) //弹出栈中的字符,与入栈的顺序相反
reverse[i]=s.Pop();
reverse[strlen(name)]='\0';
return reverse;
}
void main10_16()
{
char str[20];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -