目录
一、类的组合
? ? ? ? 1.以另一个类的对象为数据成员
? ? ? ? 2.类中类
二、C++类中默认的函数
一、类的组合
? ? ? ? 1.以另一个类的对象为数据成员
????????????????●?构造函数的写法,必须采用初始化参数列表的写法
#include<iostream>
#include<string>
using namespace std;
class Boy
{
public:
Boy(string name, int age) :name(name), age(age){}
void print()
{
cout << name << "\t" << age << endl;
}
protected:
string name;
int age;
void printData()
{
cout << name << "\t" << age << endl;
}
};
class Person
{
public:
Person(string name, string bname, int age, int bage):name(name),age(age),boy(bname,age){}
//也可以这样构建构造函数
Person(string name, int age) :name(name), age(age), boy("Mr Y", 19){}
void print()
{
boy.print();
cout << name << "\t" << age << endl;
//boy.printData();
//--->不可访问,受权限限制,boy对于Boy是类外,不可直接访问保护属性
}
protected:
string name;
int age;
Boy boy;//不能直接初始化
};
int main()
{
Person MM("Miss Du", "Mr y", 19, 20);
MM.print();
Person mm("Miss Du", 19);
mm.print();
while (1);
return 0;
}
? ? ? ? ? ? ? ? ● 类的组合构造顺序问题: 构造顺序只和定义对象顺序的有关,和初始化参数列表无关?
#include <iostream>
#include<string>
using namespace std;
class A
{
public:
A(string str) :str(str)
{
cout << "A:" << "\t";
cout << str << endl;;
}
string str;
};
class B
{
public:
B(string str) :str(str)
{
cout << "B:" << "\t";
cout << str << endl;
}
string str;
};
class C
{
public:
C(string str) :str(str)
{
cout << "C:" << "\t";
cout << str << endl;
}
string str;
};
class D
{
public:
//构造顺序只和定义对象顺序的有关,和初始化参数列表无关?
D(string stra, string strb, string strc) :b(strb), c(strc), a(stra)
{
cout << "D:" << "\t";
cout << "D" << endl << endl;;
}
A a;
B b;
C c;
};
int main()
{
D d("A", "B", "C"); //输出A:A
// B: B
// C: C
// D: D
while (1);
return 0;
}
? ? ? ? 2.类中类
? ? ? ? ? ? ? ? (1).依旧受权限限定
? ? ? ? ? ? ? ? (2).访问方式--->需要类名限定
#include<iostream>
#include<string>
using namespace std;
struct Node
{
int data;
Node* next;
Node()
{
this->next = nullptr;
}
Node(int data)//创建新结点
{
this->next = nullptr;
this->data = data;
}
};
class List
{
public:
//类中类
class iterator//迭代器-->类模仿指针行为
{
public:
iterator(Node* pMove=nullptr) :pMove(pMove){}
void operator=(Node* pMove)
{
this->pMove = pMove;
}
bool operator!=(Node* pMove)
{
return (this->pMove) != pMove;
}
iterator operator++()
{
pMove=pMove->next;
return iterator(pMove);
}
Node*& operator*()
{
return pMove;
}
protected:
Node* pMove;
};
Node* begin()
{
return headNode->next;
}
Node* end()
{
return nullptr;
}
List()
{
headNode = new Node;
}
void push_front(int data)//头插入节点
{
Node* newNode = new Node(data);
newNode->next = headNode->next;
this->headNode->next = newNode;
}
protected:
Node* headNode;
};
class A
{
public:
enum date{mon,tur,wed,thur,fri,sta,sund};
protected:
enum weather{sun,moon,afternoon};
//类中的枚举类型受权限限定
};
int main()
{
List list;
for (int i = 0; i < 5; i++)
{
list.push_front(i);
}
List::iterator iter;
for (iter = list.begin(); iter != list.end(); iter++)
{
cout << (*iter)->data<<" ";
}
cout << endl;
cout << A::date::mon << endl;
//cout << A::weather::moon;---->不可访问
while (1);
return 0;
}
二、C++类中默认的函数
? ? ? ? ● 默认构造函数
? ? ? ? ● 默认析构函数
? ? ? ? ● 默认拷贝构造函数
? ? ? ? ●? 默认赋值运算
#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
Person() = default;//默认构造函数
Person(Person& object) = default;//默认拷贝构造函数
~Person() = default;
//void print() = default;
//=default 只能出现在默认构造函数、复制/移动构造函数、复制/移动赋值运算符和析构函数中
Person& operator=(Person& object) = default;
protected:
string name;
int age;
};
int main()
{
Person MM;
Person GG;
Person mm = MM;
Person gg(GG);
MM = GG;
while (1);
return 0;
}
|