33,311
社区成员
发帖
与我相关
我的任务
分享
class Student
{
private:
string name; /// 1
public:
Student():name("aa"){} ///2
}
void main(){
cout<<"bob b:";
bob b(1);
cout<<"bm1:";
bobHisMother bm1(2);
cout<<"bm2:";
bobHisMother bm2(b);
}
#include <iostream>
using std::cout;
class testA
{
public:
testA(){cout << "Initialization a\n";}
testA(int a) {cout << "Initialization A\n";}
};
class testB
{
private:
testA testa;
public:
testB():testa(2){cout << "Initialization B\n";}
};
int main()
{
testB B;
}
#include<iostream>
using namespace std;
class bob{
public:
int a;
bob(){
a=0;
cout<<"default constructor:"<<a<<endl;
}
bob(int t){
a=t;
cout<<"argument int constructor:"<<a<<endl;
}
bob(const bob& b){
a=b.a;
cout<<"copy constructor:"<<a<<endl;
}
bob& operator =(const bob& b){
if(this==&b){
return *this;
}
a=b.a;
cout<<"assignment operator:"<<a<<endl;
return *this;
}
};
class bobHisMother{
private:
bob b;
public:
bobHisMother(int a){
b.a=a;
}
bobHisMother(const bob& bo){
b=bo;
}
};
void main(){
cout<<"bob b:";
bob b(1);
cout<<"bm1:";
bobHisMother bm1(2);
cout<<"bm2:";
bobHisMother bm2(b);
}
#include<iostream>
using namespace std;
class bob{
public:
int a;
bob(){
a=0;
cout<<"default constructor:"<<a<<endl;
}
bob(int t){
a=t;
cout<<"argument int constructor:"<<a<<endl;
}
bob(const bob& b){
a=b.a;
cout<<"copy constructor:"<<a<<endl;
}
bob& operator =(const bob& b){
if(this==&b){
return *this;
}
a=b.a;
cout<<"assignment operator:"<<a<<endl;
return *this;
}
};
class bobHisMother{
private:
bob b;
public:
bobHisMother(int a){
b.a=a;
}
bobHisMother(const bob& bo):b(bo){
}
};
void main(){
cout<<"bob b:";
bob b(1);
cout<<"bm1:";
bobHisMother bm1(2);
cout<<"bm2:";
bobHisMother bm2(b);
}