在使用包含內容的時候,對於表示類X的對象,存在著兩種主要的可選方式:
1生命一個類型為X的成員
2生命一個類型為X*或者X&的成員
如果指針值絕不改變,那麼除了效率問題和寫構造函數,析構函數的方式之外,這些選擇方案是完全等價的:
class X{
public:
X(int);
};
class C{
X a;
X *p;
X &r;
public:
C(int i,intj,int k):a(i),p(new X(j)),r(*new X(k)){}
~C(){delete p;delete &r;}
}
1包含指針
[cpp]
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(string name,int age){
this->name=name;
this->age=age;
}
Person(){}
virtual void show(){
cout<<name<<" "<<age;
}
private:
string name;
int age;
};
class Student{
public:
Student(string name,int age,string number1):p(new Person(name,age)),number(number1){}
Student(){}
void show(){
p->show();
cout<<" "<<number<<endl;
}
private:
string number;
Person *p;
};
int main(){
Student s("zhang",22,"091150");
s.show();
system("pause");
return 0;
}
<SPAN style="FONT-FAMILY: Arial; BACKGROUND-COLOR: #ffffff"></SPAN>
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(string name,int age){
this->name=name;
this->age=age;
}
Person(){}
virtual void show(){
cout<<name<<" "<<age;
}
private:
string name;
int age;
};
class Student{
public:
Student(string name,int age,string number1):p(new Person(name,age)),number(number1){}
Student(){}
void show(){
p->show();
cout<<" "<<number<<endl;
}
private:
string number;
Person *p;
};
int main(){
Student s("zhang",22,"091150");
s.show();
system("pause");
return 0;
}
2包含對象
[cpp]
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(string name,int age){
this->name=name;
this->age=age;
}
Person(){}
virtual void show(){
cout<<name<<" "<<age;
}
private:
string name;
int age;
};
class Student{
public:
Student(string name,int age,string number1):p(name,age),number(number1){}
Student(){}
void show(){
p.show();
cout<<" "<<number<<endl;
}
private:
string number;
Person p;
};
int main(){
Student s("zhang",22,"091150");
s.show();
system("pause");
return 0;
}
<SPAN style="FONT-FAMILY: Arial; BACKGROUND-COLOR: #ffffff"></SPAN>
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(string name,int age){
this->name=name;
this->age=age;
}
Person(){}
virtual void show(){
cout<<name<<" "<<age;
}
private:
string name;
int age;
};
class Student{
public:
Student(string name,int age,string number1):p(name,age),number(number1){}
Student(){}
void show(){
p.show();
cout<<" "<<number<<endl;
}
private:
string number;
Person p;
};
int main(){
Student s("zhang",22,"091150");
s.show();
system("pause");
return 0;
}
3包含引用
[cpp]
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(string name,int age){
this->name=name;
this->age=age;
}
Person(){}
virtual void show(){
cout<<name<<" "<<age;
}
private:
string name;
int age;
};
class Student{
public:
Student(string name,int age,string number1):number(number1),p(*new Person(name,age)){}
Student():p(*new Person()){} //這步不能少,一定得初始化
void show(){
p.show();
cout<<" "<<number<<endl;
}
private:
string number;
Person &p;
};
int main(){
Student s("zhang",22,"091150");
s.show();
system("pause");
return 0;
}
<SPAN style="FONT-FAMILY: Arial; BACKGROUND-COLOR: #ffffff"></SPAN>
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(string name,int age){
this->name=name;
this->age=age;
}
Person(){}
virtual void show(){
cout<<name<<" "<<age;
}
private:
string name;
int age;
};
class Student{
public:
Student(string name,int age,string number1):number(number1),p(*new Person(name,age)){}
Student():p(*new Person()){} //這步不能少,一定得初始化
void show(){
p.show();
cout<<" "<<number<<endl;
}
private:
string number;
Person &p;
};
int main(){
Student s("zhang",22,"091150");
s.show();
system("pause");
return 0;
}
特別注意包含指針和引用指針,要new一個,最好加多一個析構函數delete刪除一個,包含引用的時候,別忘了初始化。
作者:zh634455283