zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    GNU KHATA——开源的会计管理软件
    Web服务精讲–搭个 Web 服务器(二)
    据说Linuxer都难忘的25个画面
    谷歌开源运作解密
    实战Centos系统部署Codis集群服务
    这些被称为史上最 “贱” 黑客
    Linux 利器- Python 脚本编程入门(一)
    在 Ubuntu 16.04 上安装 LEMP 环境之图文向导
    Linux下6种优秀的邮件传输代理
    移动互联网期末笔记
  • 原文地址:https://www.cnblogs.com/do-your-best/p/11147888.html
Copyright © 2011-2022 走看看