zoukankan      html  css  js  c++  java
  • c++知识学习 (4)

    c++面向对象

    类继承

    class Student:public Person{

    float  score;

    }

    class <派生类名> : <继承方式> <基类名>

    派生类和基类的构造函数。

    派生类 覆盖 基类的方法。

     1 //
     2 //  Person.h
     3 //  ArrayTest
     4 //
     5 //  Created by 张学院 on 14-1-8.
     6 //  Copyright (c) 2014年 com.mix. All rights reserved.
     7 //
     8 
     9 //防止重复引用
    10 #ifndef __ArrayTest__Person__
    11 #define __ArrayTest__Person__
    12 
    13 #include <iostream>
    14 using namespace std;
    15 class Person{
    16 //---------成员变量--------
    17 public :
    18     
    19     int age;
    20     
    21 private :
    22     int weight;
    23     char  * name;
    24     char sex;
    25 //---------成员方法--------
    26 public:
    27     //----构造函数-------
    28     Person();
    29     //-----构造函数重载-----
    30     Person(int age);
    31     
    32     //------拷贝构造函数-------
    33     Person(const Person & per);
    34     
    35     //----------操作符重载----------
    36     Person & operator=(const Person &right);
    37     
    38     void setWeight(int weight);
    39     int getWeight() const;
    40     //char * getName() const;
    41     //const 指针,保证指针不被修改
    42     const char * getName() const;
    43     void setName(const char * name);
    44     void info() const;
    45     ~Person();
    46 };
    47 
    48 class Student:public Person{
    49 private:
    50     float score;
    51 public:
    52     void info() const;
    53     Student();
    54 };
    55 #endif /* defined(__ArrayTest__Person__) */
      1 //
      2 //  Person.cpp
      3 //  ArrayTest
      4 //
      5 //  Created by 张学院 on 14-1-8.
      6 //  Copyright (c) 2014年 com.mix. All rights reserved.
      7 //
      8 
      9 #include "Person.h"
     10 //------方法实现格式------
     11 //----返回值 类::方法名 (参数)------
     12 void Person::setWeight(int weight){
     13     //-----this 代表
     14     this->weight=weight;
     15 }
     16 //--------const 编译限制-------
     17 int  Person::getWeight() const{
     18     //weight++;报错
     19     return weight;
     20 }
     21 const char * Person::getName() const{
     22     
     23     return name;
     24     
     25 }
     26 void Person::setName(const char * name){
     27 
     28     strcpy(this->name, name);
     29     
     30 }
     31 void Person::info() const{
     32 
     33     printf("%s
    %d
    %c
    %d
    ",name,age,sex,weight);
     34 }
     35 //--------构造函数:初始化成员变量------------
     36 Person::Person(){
     37     printf("call the functon Person()
    ");
     38     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
     39     //strcpy(name, "a man");
     40     //在堆里分配内存,返回首地址,在堆里面分配的空间一定要记得释放内存!!!!!
     41     name = new char[255];
     42     strcpy(name, "a man");
     43     weight=60;
     44     age=20;
     45     sex='m';
     46 }
     47 //--------构造函数:重载------------
     48 Person::Person(int age){
     49     printf("call the functon Person(int age)
    ");
     50     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
     51     //strcpy(name, "a man");
     52     name = new char[255];
     53     strcpy(name, "a man");
     54     weight=60;
     55     this->age=age;
     56     sex='m';
     57     
     58 }
     59 
     60 //------拷贝构造函数-------
     61 Person::Person(const Person & person){
     62 /* 自己不实现的拷贝构造函数的话,系统生成的拷贝构造函数
     63     this->name=person.name;
     64     this->age=person.age;
     65     this->weight=person.weight;
     66     this->sex=person.sex;
     67 */
     68  // 自己实现的拷贝构造函数的话
     69     //重新分配内存
     70     this->name= new char[255];
     71     strcpy(this->name, person.name);
     72     this->age=person.age;
     73     this->weight=person.weight;
     74     this->sex=person.sex;
     75     
     76 }
     77 
     78 //-----------析构函数---------
     79 Person::~Person(){
     80     //在析构函数里面释放内存
     81     printf("per.name is %s call the functon ~Person()析构函数 
    ",name);
     82     delete [] name;
     83    
     84 }
     85 
     86 //----------操作符重载----------
     87 Person & Person::operator=(const Person &right){
     88     /* 自己不实现的操作符重载的话,系统生成的拷贝构造函数
     89     
     90     this->name=right.name;
     91     this->age=right.age;
     92     this->sex=right.sex;
     93     this->weight=right.weight;
     94       */
     95     //---------不需要再次分配内存-------
     96    // this->name= new char[255];
     97     strcpy(this->name, right.name);
     98     this->age=right.age;
     99     this->sex=right.sex;
    100     this->weight=right.weight;
    101     return *this;
    102 }
    103 
    104 //--------------student----------
    105 //-------------函数名字一样:函数覆盖----------------
    106 void Student::info() const{
    107    // printf("%s
    %d
    %c
    %d %.2f
    ",name,age,sex,weight,score);
    108     Person::info();
    109     printf("%.2f
    ",score);
    110 }
    111 //---------子类的构造函数:不需要再调用父类的构造函数------
    112 Student::Student(){
    113     score=1.00f;
    114 
    115 }
     1 //
     2 //  main.cpp
     3 //  ArrayTest
     4 //
     5 //  Created by 张学院 on 14-1-6.
     6 //  Copyright (c) 2014年 com.mix. All rights reserved.
     7 //
     8 
     9 #include <iostream>
    10 
    11 #include <string>
    12 #include "Person.h";
    13 int main()
    14 {
    15     
    16     Person per=Person();
    17     Student stu1= Student();
    18    
    19     printf("%lu,%lu 
    ",sizeof(per),sizeof(stu1));
    20     
    21     
    22     Student stu= Student();
    23     //-----public 继承:继承成员变量和成员函数-------
    24     stu.setWeight(100);
    25     stu.setName("a student");
    26     stu.info();
    27     
    28     
    29 }

    输出:

    call the functon Person()

    call the functon Person()

    24,24 

    call the functon Person()

    a student

    20

    m

    100

    1.00

    per.name is a student call the functon ~Person()析构函数 

    per.name is a man call the functon ~Person()析构函数 

    per.name is a man call the functon ~Person()析构函数 

     

    一直不清楚为什么

    printf("%lu,%lu 
    ",sizeof(per),sizeof(stu1));的结果和视频里面的不一样。

     

  • 相关阅读:
    js中常见的异步操作有哪些?
    z-index
    transition和animation的区别?
    JS 中遇到有特殊字符或者空格时会被转译该怎么办?
    url参数中有+、空格、=、%、&、#等特殊符号无法显示怎么办?
    JS如何删除对象中的某一属性?
    splice和slice、map和forEach、 filter()、reduce()的区别
    css呼吸灯效果
    mescroll—移动端精致的下拉刷新和上拉加载js框架(支持Vue)
    血淋淋的教训—将Vue项目打包成app的跨域问题
  • 原文地址:https://www.cnblogs.com/aosting/p/3512421.html
Copyright © 2011-2022 走看看