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

📄 构造函数.txt

📁 钱能主编 C++程序设计教程(第一版) 该书习题的答案代码
💻 TXT
字号:
//**************************
//**      ch12_2.cpp      **
//**************************
#include <iostream.h>

class Student
{
public:
	Student()
	{
		cout<<"constructing student."<<endl;
		semesHours=100;
		gpa=3.5;
	}
protected:
	int semesHours;
	float gpa;
};

class Teacher
{
public:
	Teacher()
	{
		cout<<"constructing teacher."<<endl;
	}
};

class TutorPair
{
public:
	TutorPair()
	{
		cout<<"constructing tutorpair."<<endl;
		noMeetings=0;
	}
protected:
	Student student;
	Teacher teacher;
	int noMeetings;
};

void main()
{
	TutorPair tp;
	cout<<"back in main."<<endl;
}

//**************************
//**      ch12_3.cpp      **
//**************************
#include <iostream.h>

class Student
{
public:
	Student()
	{
		cout<<"constructing student."<<endl;
		semesHours=100;
		gpa=3.5;
	}
	~Student()
	{
		cout<<"desturcting student."<<endl;
	}
protected:
	int semesHours;
	float gpa;
};

class Teacher
{
public:
	Teacher()
	{
		cout<<"constructing teacher."<<endl;
	}
	~Teacher()
	{
		cout<<"destructing teacher."<<endl;
	}
};

class TutorPair
{
public:
	TutorPair()
	{
		cout<<"constructing tutorpair."<<endl;
		noMeetings=0;
	}
	~TutorPair()
	{
		cout<<"destructing tutorpair."<<endl;
	}
protected:
	Student student;
	Teacher teacher;
	int noMeetings;
};

void main()
{
	TutorPair tp;
	cout<<"back in main."<<endl;
}

//**************************
//**      ch12_4.cpp      **
//**************************
#include <iostream.h>
#include <string.h>

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

void main()
{
	Student ss("qin");
}

//**************************
//**      ch12_5.cpp      **
//**************************
#include <iostream.h>
#include <string.h>

class Student
{
public:
	Student(char* pName,int xHours,float xgpa)  //the constructor can take more than one parameter
	{
		cout<<"constructing student "<<pName<<endl;
		strncpy(name,pName,sizeof(name));
		name[sizeof(name)-1]='\0';
		semesHours=xHours;
		gpa=xgpa;
	}
	~Student()
	{
		cout<<"destructing student "<<name<<endl;
	}
	void disp()
	{
		cout<<"func: Disp"<<endl;
		cout<<semesHours<<endl;
		cout<<gpa<<endl;
	}
protected:
	char name[20];
	int semesHours;
	float gpa;
};

void main()
{
	Student ss("Jenny",22,3.5);
	ss.disp();
}

//**************************
//**      ch12_6.cpp      **
//**************************
#include <iostream.h>

class Tdate
{
public:
	Tdate();
	Tdate(int d);
	Tdate(int m,int d);
	Tdate(int m,int d,int y);
protected:
	int month;
	int day;
	int year;
};

Tdate::Tdate()
{
	month=4; day=15; year=1995;
	cout<<month<<"/"<<day<<"/"<<year<<endl;
}
Tdate::Tdate(int d)
{
	month=4; day=d; year=1996;
	cout<<month<<"/"<<day<<"/"<<year<<endl;
}
Tdate::Tdate(int m,int d)
{
	month=m; day=d; year=1997;
	cout<<month<<"/"<<day<<"/"<<year<<endl;
}
Tdate::Tdate(int m,int d,int y)
{
	month=m; day=d; year=y;
	cout<<month<<"/"<<day<<"/"<<year<<endl;
}

void main()
{
	Tdate aday;
	Tdate bday(10);
	Tdate cday(2,12);
	Tdate dday(1,2,1998);
}

//**************************
//**      ch12_7.cpp      **
//**************************
#include <iostream.h>

class Tdate
{
public:
	Tdate();
	Tdate(int d);
	Tdate(int m,int d);
	Tdate(int m,int d,int y);
protected:
	int month;
	int day;
	int year;
	void Init(int m,int d,int y)
	{
		month=m; day=d; year=y;
		cout<<month<<"/"<<day<<"/"<<year<<endl;
	}
};

Tdate::Tdate()
{
	Init(4,15,1995);
}
Tdate::Tdate(int d)
{
	Init(4,d,1996);
}
Tdate::Tdate(int m,int d)
{
	Init(m,d,1997);
}
Tdate::Tdate(int m,int d,int y)
{
	Init(m,d,y);
}


void main()
{
	Tdate aday;
	Tdate bday(10);
	Tdate cday(2,12);
	Tdate dday(1,2,1998);
}

//**************************
//**      ch12_8.cpp      **
//**************************
#include <iostream.h>

class Tdate
{
public:
	Tdate(int m=4,int d=15,int y=1995)
	{
		month=m; day=d; year=y;
		cout<<month<<"/"<<day<<"/"<<year<<endl;
	}
protected:
	int month;
	int day;
	int year;
};

void main()
{
	Tdate aday;
	Tdate bday(2);
	Tdate cday(2,12);
	Tdate dday(1,2,1998);
}

//***************************
//**      ch12_10.cpp      **
//***************************
#include <iostream.h>
#include <string.h>

int nextStudentID=0;
class StudentID
{
public:
	StudentID()
	{
		value=++nextStudentID;
		cout<<"Assigning student id "<<value<<endl;
	}
	~StudentID()
	{
		--nextStudentID;
		cout<<"Destructing id "<<value<<endl;
	}
protected:
	int value;
};

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

void main()
{
	Student s("Randy");
}

//***************************
//**      ch12_11.cpp      **
//***************************
#include <iostream.h>
#include <string.h>

int nextStudentID=0;
class StudentID
{
public:
	StudentID(int id=0)
	{
		value=id;
		cout<<"Assigning student id "<<value<<endl;
	}
	~StudentID()
	{
		--nextStudentID;
		cout<<"Destructing id "<<value<<endl;
	}
protected:
	int value;
};

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

void main()
{
	Student s("Randy",9918);
}

//***************************
//**      ch12_12.cpp      **
//***************************
#include <iostream.h>
#include <string.h>

int nextStudentID=0;
class StudentID
{
public:
	StudentID(int id=0)
	{
		value=id;
		cout<<"Assigning student id "<<value<<endl;
	}
	~StudentID()
	{
		--nextStudentID;
		cout<<"Destructing id "<<value<<endl;
	}
protected:
	int value;
};

class Student
{
public:
	Student(char* pName="noName",int ssID=0):id(ssID)  //remember this kind of assigning space for protected member
	{
		cout<<"constructing student "<<pName<<endl;
		strncpy(name,pName,sizeof(name));
		name[sizeof(name)-1]='\0';
		//StudentID id(ssID);  //wrong using of initialization id
	}
	~Student()
	{
		cout<<"destructing student "<<name<<endl;
	}
protected:
	char name[20];
	StudentID id;
};

void main()
{
	Student s("Randy",9918);
	//Student t("Jenny");
}
//***************************
//**      ch12_14.cpp      **
//***************************
#include <iostream.h>
#include <string.h>

class SmallOne
{
public:
	SmallOne(int sma)
	{
		cout<<"SmallOne constructing with a value of "<<sma<<endl;
	}
};

void fn(int n)
{
	static SmallOne sm(n);  //note: here use the static, see the result~
	cout<<"In function fn with n="<<n<<endl;
}

void main()
{
	fn(10);
	fn(20);
}

⌨️ 快捷键说明

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