• C++面向对象实践


    实践如下:

    class Person{
    
    private:
        int age;
        char name[25];
        int hight;
    
    public:
        Person(int age, int hight, char* name);
        int getAge();
        char* getName();
        int getHight(){
            return hight;
        }
    };
    #include "Person.h"
    
    
    Person::Person(int age, int hight, char* name1){
        Person::age = age;
        Person::hight = hight;
        Person::name = name1;
    }
    
    int Person::getAge(){
        return age;
    }
    
    char* Person::getName(){
        return name;
    }
    #include <iostream>
    
    #include "Person.h"
    
    using namespace std;
    
    int main(){
    
        cout << "面向对象实践:" << endl;
    
    
        cout << "end." << endl;
    
        return 0;
    }

     第二版:

    class Person{
    
    private:
        int age;
        char name[25];
        int hight;
    
    public:
        // 静态常量不允许修改
        const static int SVar = 100;
        const static int Default_Age = 30;
        const static int Default_Hight = 166;
    
    
        // static 作为默认参数
        Person(int age = Default_Age, int hight = Default_Hight){
            this->age = age;
            this->hight = hight;
        }
        Person(Person &p);
        Person(Person *p);
        ~Person();
        Person(int age, int hight, char name[25]);
        int getAge();
        char* getName();
        int getHight(){
            return this->hight;//(*this).hight;
        }
    };
    #include <iostream>
    #include <string.h>
    
    #include "Person.h"
    
    Person::Person(int age, int hight, char name[25]){
        Person::age = age;
        Person::hight = hight;
        strcpy(Person::name, name);
    }
    Person::Person(Person &p){
        age = p.age;
        hight = p.hight;
    }
    Person::Person(Person *p){
        age = p->age;
        hight = p->hight;
    }
    Person::~Person(){
        //delete[] name;
        std::cout<<"...~
    ";
    }
    int Person::getAge(){
        return age;
    }
    
    char* Person::getName(){
        return name;
    }
    #include <iostream>
    
    #include "Person.h"
    
    using namespace std;
    
    int main(){
    
        cout << "面向对象实践1:" << endl;
    
        Person p(1,1);
    
        cout << "age:" << p.getAge()<< endl;
        cout << "hight:" << p.getHight() << endl;
    
        char name[25] = "1212121212q";
        Person pp(22,22,name);
        Person *p1 = &pp;
    
        cout << "age1:" << p1->getAge()<< endl;
        cout << "hight1:" << p1->getHight() << endl;
        cout << "name1:" << p1->getName() << endl;
    
        Person p2(p);
        cout << "age2:" << p2.getAge()<< endl;
        cout << "hight2:" << p2.getHight() << endl;
    
        Person p3(p1);
        cout << "age3:" << p3.getAge()<< endl;
        cout << "hight3:" << p3.getHight() << endl;
    
    
    //    Person p4(NULL,NULL);
    //    cout << "age4:" << p4.getAge()<< endl;
    //    cout << "hight4:" << p4.getHight() << endl;
    
        cout << "Person::SVar:" << Person::SVar << endl;
    
        cout << "end1." << endl;
    
        return 0;
    }
  • 相关阅读:
    c#代码:使用假设的方法遍历解决“谁养鱼”问题(据说是爱因斯坦所出的一道推理题) 无为而为
    远洋地暖的使用步骤
    合伙人四大原则
    model y搭载60度磷酸铁锂电池的续航表现
    model3家用充电桩按220V还是380V区别?
    食用油的挑选标准
    职责链模式(Chain of Responsibility)
    通用数据链接(UDL)的用法
    Oracle REGEXP_INSTR 用法
    访问者模式(Visitor)
  • 原文地址:https://www.cnblogs.com/do-your-best/p/11147888.html
Copyright © 2020-2023  润新知