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

📄 demo_constructor_10.cpp

📁 对于一个初涉VC++的人来书
💻 CPP
字号:

//***************************************************

# include <iostream.h>
# include <string.h>

class SillyClass
{
public:
	SillyClass(int& i):ten(10),refI(i) //冒号语法
	{
		cout<<"Assigning student id "<<ten<<" "<<refI<<endl;
	}
protected:
	const int ten;
	int& refI;
};

class SillyClass1
{
public:
	SillyClass1()
	{
		d=10;
		cout<<"Assigning student id1 "<<d<<endl;
	}
protected:
	int d;
};

class SillyClass2
{
public:
	SillyClass2():d(10)
	{
		cout<<"Assigning student id2 "<<d<<endl;
	}
protected:
	int d;
};

class SillyClass3
{
public:
	SillyClass3():d(10){}
protected:
	int d;
};

class Student
{
public:
	Student(char* pName="noName")
	{
		cout<<"Constructing student: "<<pName<<endl;
		strncpy(name,pName,sizeof(name));
		name[sizeof(name)-1]='\0';
	}
protected:
	char name[20];
};

void main()
{
	int i1=6, i2(8);

	SillyClass sc1(i1),sc2(i2);

	SillyClass1 u;
	SillyClass2 v;
	SillyClass3 w;

	Student s="Randy",t("Jenny");
}

/*
Assigning student id 10 6
Assigning student id 10 8
Assigning student id1 10
Assigning student id2 10
constructing student: Randy
constructing student: Jenny
Press any key to continue
*/

⌨️ 快捷键说明

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