?? 堆與拷貝構造函數.txt
字號:
//**************************
//** ch14_1.cpp **
//**************************
#include <iostream.h>
#include <string.h>
class Student
{
public:
Student(char* pName="noName",int ssId=0)
{
id=ssId;
strcpy(name,pName);
cout<<"Constructing new student "<<pName<<endl;
}
Student(Student &s)
{
cout<<"Constructing copy of "<<s.name<<endl;
strcpy(name,"copy of ");
strcat(name,s.name);
id=s.id;
}
void dis()
{
cout<<name<<endl;
}
~Student()
{
cout<<"Destrcuting "<<name<<endl;
}
protected:
char name[40];
int id;
};
void fn(Student s)
{
cout<<"In function fn()"<<endl;
cout<<"now the name of randy is ";
s.dis();
}
void main()
{
Student randy("Randy",1234);
randy.dis();
cout<<"Calling fn()"<<endl;
fn(randy);
cout<<"Returned from fn()"<<endl;
}
//**************************
//** ch14_2.cpp **
//**************************
#include <iostream.h>
#include <string.h>
class Student
{
public:
Student(char* pName="no name")
{
cout<<"Constructing new student "<<pName<<endl;
strncpy(name,pName,sizeof(name));
name[sizeof(name)-1]='\0';
}
Student(Student &s)
{
cout<<"Constructing copy of "<<s.name<<endl;
strcpy(name,"copy of ");
strcat(name,s.name);
}
void dis()
{
cout<<name<<endl;
}
~Student()
{
cout<<"Destructing "<<name<<endl;
}
protected:
char name[40];
};
class Tutor
{
public:
Tutor(Student &s):student(s)
{
cout<<"Constructing tutor"<<endl;
}
void disT()
{
cout<<"In tutor"<<endl;
student.dis();
}
protected:
Student student;
};
void fn(Tutor tutor)
{
cout<<"In function fn()"<<endl;
//tutor.disT();
}
void main()
{
Student randy("Randy");
Tutor tutor(randy);
//tutor.disT();
cout<<"Calling fn()"<<endl;
fn(tutor);
cout<<"Returned from fn()"<<endl;
}
//**************************
//** ch14_3.cpp **
//**************************
#include <iostream.h>
#include <string.h>
class Person
{
public:
Person(char* pN)
{
cout<<"Constructing "<<pN<<endl;
pName=new char[strlen(pN)+1];
if(pName!=0)
{
strcpy(pName,pN);
}
}
~Person()
{
cout<<"Destructing "<<pName<<endl;
pName[0]='\0';
delete pName;
}
protected:
char* pName;
};
void main()
{
Person p1("Randy");
Person p2=p1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -