美文网首页
09 类的构造 析构

09 类的构造 析构

作者: LuckTime | 来源:发表于2016-06-18 09:55 被阅读14次

    //-------------------类的继承-------------
    //继承作用,使一个类继承另一个类的所有属性。。
    //当继承时,通常小范围继承大范围的元素。
    //eg: 日用品 ,零食,家电 都集成商品的属性元素。。在家电中,电视,冰箱,电脑继承电器的属性

    //-----------------------------------------
    //-----------------------------------------
    //-----------------------------------------
    //-----------------------------------------
    //-----------------------------------------
    //---------组合的概念 ---------------------
    //电脑由液晶屏,主机,鼠标,键盘组成-------
    //由组成的新对象------------------
    //电脑之中{由}这些元素属性{组成},并不是说主机就是电脑,鼠标就是电脑。。-----》组成
    //而笔记本,一体机{是}电脑。---------------》类的继承

    //基类-> 旧类->父类
    //派生类 ->新类->子类

    //派生和继承是同一件事情,只是看的角度不同
    //eg:父类看子类为派生,子类看父类为继承

    include <iostream>

    include <string>

    include "stdafx.h"

    using namespace std;
    class Person
    {
    char name[20];
    bool gender;
    int age;

    public: //公有
    Person( char* name, bool gender, int age ) : gender(gender),age(18)
    {
    if(strlen(name) > 19)
    {
    cout << "error !,have a long len name" <<endl;
    name[19] = '\0';
    }
    strcpy(this->name,name);
    }

    void setAge(int age) //构造函数,才能初始化函数值。。普通函数,不可以
    {
    if(age<0)
    cout << "error,the age < 0!! " <<endl;
    else if(age >200 )
    cout << "error,the age >200!! " <<endl;
    else
    this->age = age;
    }

    void show()
    {
    cout << " my name is "<< name <<endl;
    cout << " is "<< (gender? "cool":"beautiful") <<endl;
    cout << " and my age is "<< age <<endl;
    }
    };

    int main()
    {
    Person p1("灌饼",true,23);
    Person p2("月饼",false,23);
    Person p3("烧饼",true,23);
    p1.setAge(20);
    p2.setAge(20);
    p3.setAge(29);
    p1.show();
    p2.show();
    p3.show();

    return 0;
    }

    相关文章

      网友评论

          本文标题:09 类的构造 析构

          本文链接:https://www.haomeiwen.com/subject/panvdttx.html