栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++类和对象

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++类和对象

1.1 定义类

我们首先声明一个游戏类。
访问权限问题:
1.private成员只能被本类成员(类内)和友元访问,不能被派生类访问;
2.protected成员与private成员具有相似性,不同的是它可以被派生类访问。
如果不写访问权限,默认为private。

#include 

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;
    }
    void show()
    {
        cout << first << ' ' << second << ' ' << third << endl;
    }
};

int main()
{
    Game Yin("Noshir Dalal","DDTank","LOL");
    Yin.show();
    return 0;
}

创造了构造函数,一般是用于初始化。如果没有对参数进行赋值则无法直接声明变量而不传参。如下面这种初始化是错误的。

#include 

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;
    }
    void show()
    {
        cout << first << ' ' << second << ' ' << third << endl;
    }
};

int main()
{
    Game Yin; // 错误的
    Yin.show();
    return 0;
}

而给参数赋值之后就可以这样运算。

#include 

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;
    }
    void show()
    {
        cout << first << ' ' << second << ' ' << third << endl;
    }
};

int main()
{
    Game Yin;
    Yin.show();
    return 0;
}

析构函数是在函数结尾时运行的函数。类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。例如下面的例子。
析构函数调用的顺序与构造函数调用的顺序相反。

#include 

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;
    }
    void show()
    {
        cout << first << ' ' << second << ' ' << third << endl;
    }
    ~Game()
    {
        cout << "Game析构函数" << endl;
    }
};

int main()
{
    Game Yin("Noshir Dalal","DDTank","LOL");
    Yin.show();
    return 0;
}

运行结果:

拷贝构造函数:如果在类中没有定义拷贝构造函数,编译器会自行定义一个。如果类带有指针变量,并有动态内存分配,则它必须有一个拷贝构造函数。拷贝构造函数的最常见形式如下:

#include 
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;
}
1.2 定义对象

定义对象指针。

#include 

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");
    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 对象成员

对象成员,对象成员的初始化放在初始化列表后面。

#include 

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;
    }
};
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;
}
1.4 const

常量,防止被修改。例如,我对Person类中的show函数里面进行修改name值是允许的,但如果,我们这样定义:

void show() const
{
	// name = "shit";错误的,不可以被执行
	cout << name << ' ' << gender << ' ' << age << endl;
    game.show();
}

此时show为常成员函数。如果我创建一个const常对象,常对象只能调用常成员函数。常对象,可以在调用构造函数是定义变量,此时常对象类内全为常变量。

1.5 static

静态变量,只能在类外面定义。在下面的例子中,所有成员共享一个static变量。

#include 

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;
    }
};
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;
}
1.5 friend

访问私有变量,申明一个类外函数为友元函数,它就可以访问私有变量。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/968981.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号