⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memberofclass.cpp

📁 这我们老师对是面向对象程序设计(清华大学出版社)一书制作的PPT
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	cout<<"输入一个字符串:";
	cin>>str;
	cout<<"将该字符串的反序为:"<<ReverseName(str)<<'\n';
}


//【例10.17】 对象成员

class A17
{
	int x,y;
public:
    A17(){   }
	A17(int a,int b)
	{    x=a;y=b;
	    cout<<"调用类A17的构造函数!"<<endl;
	}
	void show( )
	{ cout<<"x="<<x<<'\t'<<"y="<<y<<'\n';}
    ~A17(){cout<<"调用类A17的析构函数!\n";}
};

class B17
{
	int Length,Width;
public:
    B17(){    }
	B17(int a,int b)
	{   Length=a;Width=b;
	   cout<<"调用了类B17的构造函数!"<<endl;
	}
	void show( )
	{ cout<<"Length="<<Length<<'\t'<<"width="<<Width<<'\n';}
    ~B17(){cout<<"调用类B的析构函数!\n";}
};

class C17
{
	int r,High;
	A17 a1;                                         //D
	B17 b1;                                         //E
public:
	C17(int a,int b,int c,int d):a1(c,d),b1(500,600)       //F
	{   r=a;High=b;
	   cout<<"调用了类C27的构造函数!"<<endl;
	}
	void show( )
	{
		cout<<"r="<<r<<'\t'<<"High="<<High<<'\n';
		a1.show( );
		b1.show( );
	}
    ~C17(){cout<<"调用类C的析构函数!\n";}
};

void main10_17(void)
{
	C17 c1(100,200,300,400);                        //G
	c1.show( );
}

//【例10.18】 利用成员函数设置对象成员中的成员数据

class CArea
{
	int x,y;
public:
	void set(int i,int j){
		x=i;
		y=j;
	}
	int get(){return x*y;}
};

class Volume{
	int high;
	CArea s1;
public:
	void set(int i,int j,int k)
	{   high=k;
		s1.set(i,j);       //通过成员s1中的公有成员函数设置s1中的成员数据
	}
	int get(){  return high*s1.get();	}
	void print()
	{
		cout<<"该面积="<<s1.get()<<endl;
		cout<<"该体积="<<get()<<endl;
	}
};

void main10_18(void)
{
	Volume v1;           //A
	v1.set(20,10,30);
	v1.print();
}


//【例10.19】const对象的定义和使用

class A19
{
    int a;
public:
	int b;
	A19(int i,int j);
	void Set(int i,int j)
	{a=i;b=j;}
    void Print() const       //const成员函数
    {  cout<<"a="<<a<<"\tb="<<b<<'\n';  }
};

A19::A19(int i,int j)
{	
	a=i;
	b=j;
}

void main10_19()
{
    A19 const a1(1,2);      	//创建const对象
    const A19 a2(3,4);
    a1.Print();           	//调用const成员函数
    a2.Print();
    cout<<a2.b<<'\n';    	//调用公有成员数据
    //a1.b=10;           	//A
    //a1.Set(10,20);       	//B
}


//【例10.20】 const成员函数的定义和使用

class CSample20	
{
   	int a, b;
public:
	CSample20 (int x,int y)
	{	a=x;	b=y; 	}
	~CSample20( ){}
	void PrintAB( );
	void PrintAB( ) const;         //const不能省
    const void Test(){   }
};

void CSample20::PrintAB( )
{
	cout<<"调用函数CSample20::PrintAB( )\n";
	cout<<"A="<<a<<'\t'<<"B="<<b<<endl;
}

void CSample20::PrintAB( ) const     //const不能省
{
	cout<<"调用函数CSample20::PrintAB( ) const!\n";
    cout<<"A="<<a<<'\t'<<"B="<<b<<endl;
    //a=2;                         //A
	//Test();                       //B
}

void main10_20( )
{
    CSample20 s1(11,22);
	CSample20 const s2(33,44);
	s1.PrintAB( );
	s2.PrintAB( );          //s2只能访问const成员函数
}


//【例10.21】 const成员数据的定义和使用

class CIncrement
{
	int count;
	const int increment;
public:
	CIncrement(int c=0,int i=1);
	void addIncrement(){count+=increment;}
	void Print() const;
};

CIncrement::CIncrement(int c,int i):increment(i)  	//A
{	
	count=c;  
}

void CIncrement::Print() const
{
	//increment=2;     					//B
	cout<<"count="<<count<<"\tincrement="<<increment<<'\n';
}

void main10_21()
{
	CIncrement value(10,5);
	cout<<"Before incrementing:";
	value.Print();
	for(int j=1;j<=3;j++){
		value.addIncrement();
		cout<<"After increment "<<j<<":";
		value.Print();
	}
}


//【例10.22】 用友元计算长方体的面积

class CBox
{
    int i,j;
public:
	CBox(int a=10,int b=10)
	{	i=a;	j=b;	}
	friend int get_area(CBox &);       	//A  声明友元函数
	friend int get_default_area();       	//B
};

int get_area(CBox &s)                	//定义友元函数
{  return s.i*s.j;  }                  	//C

int get_default_area()
{
	CBox t;                        	//D
	return t.i*t.j;                    	//E
}

void main10_22()
{
	cout<<"矩形面积的缺省值为:"<<get_default_area()<<'\n';
    CBox s1(18,12);	
	cout<<"矩形面积为:"<<get_area(s1)<<'\n';
}


//【例10.23】友元函数的各种形式及其应用

#define STRMAX 100         //字符串的最大长度

class CMyClass
{
	int x , y;
	char *string;
    friend void Print(CMyClass &);    			//在私有区域声明友元函数
public:
	CMyClass(int,int,char *);
	friend int Sum(CMyClass &c){return c.x+c.y;}   	//在类体内部定义友元函数
	friend int StrLength(CMyClass &c)
	{  return strlen(c.string);  }
	friend void SetData(CMyClass &,int,int,char *);
	~CMyClass(){  delete []string;  }
};

CMyClass::CMyClass(int a,int b,char *str)
{
	x=a,y=b;
	string=new char[STRMAX];
	strcpy(string,str);	
}

void Print(CMyClass &c)
{	cout<<"x="<<c.x<<"\ty="<<c.y<<"\tstring:"<<c.string<<'\n'; }

void SetData(CMyClass &c,int a,int b,char *str)	//通过友元函数更改对象的私有属性
{
	c.x=a;
	c.y=b;
	strcpy(c.string,str);
}

void main10_23(void)
{
	CMyClass c1(10,20,"China!");
	Print(c1);
	cout<<"The sum is:"<<Sum(c1)<<'\n';
	cout<<"The string length is:"<<StrLength(c1)<<'\n';	
	SetData(c1,40,60,"Computer!");
	Print(c1);
}



//【例10.24】 把一个类的成员说明为另一个类的友元函数

class A242;        //类A2的引用性说明
class A241{	
	int a,b;
public:
	A241(int i,int j){ a=i;b=j; }
	int Geta(){ return a; }
	int Getb(){ return b; }
	void Change(A242 &);          		//A
};

class A242{
	int c,d;
public:
	A242(int i,int j){c=i, d=j;}
	int Getc(){ return c; }
	int Getd(){ return d; }
	friend void A241::Change(A242 &);    	//A1中的成员函数作为友元
};

void A241::Change(A242 &a1)
{ a=a1.c; b=a1.d;}

void main10_24()
{
	A241 a1(1,2);
	A242 a2(3,4);
	cout<<"a1.a="<<a1.Geta()<<'\t'<<"a1.b="<<a1.Getb()<<endl;
	cout<<"a2.c="<<a2.Getc()<<'\t'<<"a2.c="<<a2.Getd()<<endl;
	a1.Change(a2);
	cout<<"a1.a="<<a1.Geta()<<'\t'<<"a1.b="<<a1.Getb()<<endl;
}


//【例10.25】用友元类实现堆栈的压入和弹出。

class CStack25;            	//类CStack的引用性说明
class CNode{            	//定义结点类
	int data;
	CNode *prev;
public:
	CNode(int d,CNode *n){
		data=d;
		prev=n;}
	friend class CStack25;     	//将类CStack声明为类SNode的友元类
};

class CStack25{          		//定义栈类
	CNode *top;       		//栈头
public:
	CStack25(){top=NULL;}
	void push(int i);    		//将值压入栈
	int pop();         		//从栈中弹出一个值
};

void CStack25::push(int i)
{
	CNode *n=new CNode(i,top);
	top=n;
}

int CStack25::pop()
{
	CNode *t=top;
	if(top){ 
		top=top->prev;
		int c=t->data;       		//通过CNode的对象t访问类CNode的私有成员
		delete t;
		return c;
	}
	return 0;
}

main10_25()
{
	int c;
	CStack25 s;
	cout<<"请向栈内输入10个整数:";
	for(int i=0;i<10;i++)       		//输入10个整数,并依次压入栈
	{
		cin>>c;	
		s.push(c);
	}
	cout<<"依次弹出栈内中的整数:";
	for(i=0;i<10;i++)             	//将栈中的数依次弹出
		cout<<s.pop()<<"  ";
	cout<<endl;
	return 1;
}


//【例10.26】静态成员数据的定义及应用

#include <stdlib.h>

class CCounter
{
	static int count;        //定义静态成员数据
	int objnumber;         //表示对象编号
public:
	CCounter()
	{
		count++;
		objnumber=count;
	}
	void Show()
	{cout<<"obj"<<objnumber<<'\t'<<"count="<<count<<'\n';}
};

int CCounter::count=0;    //A    在类定义外声明静态成员数据并初始化

void main10_26()
{
	CCounter obj1;
	obj1.Show();
	cout<<"----------------\n";
	CCounter obj2;
	obj1.Show();
	obj2.Show();
	cout<<"----------------\n";
	CCounter obj3;
	obj1.Show();
	obj2.Show();
	obj3.Show();
}


//【例10.27】 分析程序输出的结果

#include<iostream.h>

class A27
{
	int i;
	static int x;
public:
	static int y;
	A27(int a,int b,int c)
	{i=a,	x=b,	y=c;	}
	void print()
	{cout<<"i="<<i<<'\t'<<"x="<<x<<'\t'<<"y="<<y<<endl;}
};

int A27::x=0;
int A27::y=0;

void main10_27()
{
	cout<<"y="<<A27::y<<endl;             	//B
	A27 a(11,22,33);
	a.print();
	A27 b(100,200,300);
	a.print();
	b.print();
	A27::y=400;                            	//C
	cout<<a.y<<"  "<<b.y<<endl;
	b.print();
}


//【例10.28】静态成员数据的生存期

/*#include<iostream.h>

class A28
{
	int i;
public:
	A28(int x){i=x;cout<<"x="<<i<<"\t调用构造函数A28()\n";}
	~A28(){cout<<"x="<<i<<"\t调用析构函数~A28()\n";}
};

class B28
{
	static A28 a;        //声明静态成员数据
	static A28 c;
public:
	B28(){cout<<"调用构造函数B28()\n";}
	~B28(){cout<<"调用析构函数~B28()\n";}
};

A28 B28::a(10);			//C   在类体外声明静态成员数据并初始化
A28 B28::c(5);				//D
A28 a1(20);				//定义用户自定义类型的全局变量并初始化

void main10_28()
{
	cout<<"main()函数开始!\n";
	B28 b;
	cout<<"main()函数结束!\n";
}*/


//【例10.29】 静态成员函数的定义和使用

void num_Show();

class CComputer
{
	float price;
	static float total;
public:
	static int num;        	//创建对象的个数
	CComputer(float i) 
	{
		price=i;
		total+=i;
		num++;        	//每一次创建对象时对象的个数加1
	}
	void show()
	{cout<<"The computer price is:"<<price<<endl;}
	static void t_show()			//静态成员函数
	{ 
	    num_Show();        	//静态成员函数访问外部定义的函数
		cout<<"total price is:"<<total<<endl;   //访问静态数据成员total
	}
};

float CComputer::total=0;
int CComputer::num=0;

void num_Show()   //输出类CComputer静态数据成员num
{cout<<"total number is:"<<CComputer::num<<endl;}

void main10_29()
{
	CComputer::t_show();    //通过类名直接访问静态成员函数
	CComputer c1(3500);
	c1.show();
	c1.t_show();
	CComputer c2(4500);
	c2.show();	
	CComputer::t_show();    //A
}


//【例10.30】在静态成员函数中通过对象访问类的非静态成员。

class A30
{
	int x;
	static int y;
public:
	A30(int x1,int x2)
	{
		x=x1;
		y=y+x2;
	}
	static void show1();
	static void show2(A30 a);
};

void A30::show1()
{	cout<<"Y="<<y<<endl;   }          //直接访问静态数据

void A30::show2(A30 a)
{cout<<"X="<<a.x<<"\t"<<"Y="<<y<<endl; } 	//B 

int A30::y=6;

void main10_30()
{
	A30 a1(11,22);
	a1.show1();                      //通过对象名访问
	A30::show2(a1);                    //通过类名访问
	A30 a2(33,44);
	A30::show1();                     //通过类名访问
	a2.show2(a2);                   //通过对象名访问
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -