我们首先声明一个游戏类。
访问权限问题:
1.private成员只能被本类成员(类内)和友元访问,不能被派生类访问;
2.protected成员与private成员具有相似性,不同的是它可以被派生类访问。
如果不写访问权限,默认为private。
#includeusing namespace std; class Game{ private: string first; string second; string third; public: Game (string a, string b, string c) { first = a; second = b; third = c; } void show() { cout << first << ' ' << second << ' ' << third << endl; } }; int main() { Game Yin("Noshir Dalal","DDTank","LOL"); Yin.show(); return 0; }
创造了构造函数,一般是用于初始化。如果没有对参数进行赋值则无法直接声明变量而不传参。如下面这种初始化是错误的。
#includeusing namespace std; class Game{ private: string first; string second; string third; public: Game (string a, string b, string c) { first = a; second = b; third = c; } void show() { cout << first << ' ' << second << ' ' << third << endl; } }; int main() { Game Yin; // 错误的 Yin.show(); return 0; }
而给参数赋值之后就可以这样运算。
#includeusing namespace std; class Game{ private: string first; string second; string third; public: Game (string a = "", string b = "", string c = "") { first = a; second = b; third = c; } void show() { cout << first << ' ' << second << ' ' << third << endl; } }; int main() { Game Yin; Yin.show(); return 0; }
析构函数是在函数结尾时运行的函数。类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。例如下面的例子。
析构函数调用的顺序与构造函数调用的顺序相反。
#includeusing namespace std; class Game{ private: string first; string second; string third; public: Game (string a = "", string b = "", string c = "") { first = a; second = b; third = c; } void show() { cout << first << ' ' << second << ' ' << third << endl; } ~Game() { cout << "Game析构函数" << endl; } }; int main() { Game Yin("Noshir Dalal","DDTank","LOL"); Yin.show(); return 0; }
运行结果:
拷贝构造函数:如果在类中没有定义拷贝构造函数,编译器会自行定义一个。如果类带有指针变量,并有动态内存分配,则它必须有一个拷贝构造函数。拷贝构造函数的最常见形式如下:
#include1.2 定义对象using namespace std; class Game{ private: string first; string second; string third; public: Game (string a = "", string b = "", string c = "") { first = a; second = b; third = c; cout << "Game构造函数" << endl; } Game (const Game &t) { first = t.first; second = t.second; third = t.third; cout << "Game拷贝构造函数" << endl; } void show() { cout << first << ' ' << second << ' ' << third << endl; } ~Game() { cout << "Game析构函数" << endl; } }; int main() { Game Yin("Noshir Dalal","DDTank","LOL"); Yin.show(); Game Yu = Yin; // 注意 上式与 Game YU; Yu = Yin; 是不一样的,此时没有调用拷贝构造函数。 Yu.show(); return 0; }
定义对象指针。
#includeusing namespace std; class Game{ private: string first; string second; string third; public: Game (string a = "", string b = "", string c = "") { first = a; second = b; third = c; cout << "Game构造函数" << endl; } Game (const Game &t) { first = t.first; second = t.second; third = t.third; cout << "Game拷贝构造函数" << endl; } void show() { cout << first << ' ' << second << ' ' << third << endl; } ~Game() { cout << "Game析构函数" << endl; } }; int main() { Game Yin("Noshir Dalal","DDTank","LOL"); Game *p; p = &Yin; p->show(); (*p)->show(); return 0; }
定义对象数组,把它当做一般数组处理就行。初始化可以采取下列表达式
Game q[3] = {Game("Noshir Dalal","DDTank","LOL"),Game("Noshir Dalal","DDTank","LOL"),Game()};
对象引用。
Game &w = Yin; w.show();
动态调用对象。
Game *qq = new Game("Noshir Dalal","DDTank","LOL");
qq->show();
delete qq;
指针和引用是不创建对象的。
1.3 对象成员对象成员,对象成员的初始化放在初始化列表后面。
#include1.4 constusing namespace std; class Game{ private: string first; string second; string third; public: Game (string a = "", string b = "", string c = "") { first = a; second = b; third = c; cout << "Game构造函数" << endl; } Game (const Game &t) { first = t.first; second = t.second; third = t.third; cout << "Game拷贝构造函数" << endl; } void show() { cout << first << ' ' << second << ' ' << third << endl; } ~Game() { cout << "Game析构函数" << endl; } }; class Person { private: string name, gender; int age; Game game; public: Person(string n, string g, int a, string f, string s, string t):game(f, s, t) { name = n; gender = g; age = a; cout << "Person构造函数" << endl; } void show() { cout << name << ' ' << gender << ' ' << age << endl; game.show(); } ~Person() { cout << "Person析构函数" << endl; } }; int main() { Person p("Yin", "male", 20, "Noshir Dalal","DDTank","LOL"); p.show(); return 0; }
常量,防止被修改。例如,我对Person类中的show函数里面进行修改name值是允许的,但如果,我们这样定义:
void show() const
{
// name = "shit";错误的,不可以被执行
cout << name << ' ' << gender << ' ' << age << endl;
game.show();
}
此时show为常成员函数。如果我创建一个const常对象,常对象只能调用常成员函数。常对象,可以在调用构造函数是定义变量,此时常对象类内全为常变量。
1.5 static静态变量,只能在类外面定义。在下面的例子中,所有成员共享一个static变量。
#include1.5 friendusing namespace std; class Game{ private: string first; string second; string third; public: Game (string a = "", string b = "", string c = "") { first = a; second = b; third = c; cout << "Game构造函数" << endl; } Game (const Game &t) { first = t.first; second = t.second; third = t.third; cout << "Game拷贝构造函数" << endl; } void show() { cout << first << ' ' << second << ' ' << third << endl; } ~Game() { cout << "Game析构函数" << endl; } }; class Person { private: string name, gender; int age; Game game; public: static int count; Person(string n, string g, int a, string f, string s, string t):game(f, s, t) { name = n; gender = g; age = a; cout << "Person构造函数" << endl; count ++; } void show() { cout << name << ' ' << gender << ' ' << age << endl; game.show(); cout << "Person的个数" << count << endl; } ~Person() { cout << "Person析构函数" << endl; count --; } static void showCount()//静态函数里面只能访问静态成员 { cout << "count:" << count << endl; } }; int Person::count = 0; int main() { Person p1("Yin", "male", 20, "Noshir Dalal","DDTank","LOL"), p2("Yin", "male", 20, "Noshir Dalal","DDTank","LOL"); Person p3 = new Person("Yin", "male", 20, "Noshir Dalal","DDTank","LOL"); cout << count << endl; delete p3; count << cout << endl; Person::showCount(); p.show(); return 0; }
访问私有变量,申明一个类外函数为友元函数,它就可以访问私有变量。



