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

    基类与派生类构造函数

    调用顺序

    1 基类构造函数

    2 成员类对象构造函数:比如string对象

    3 派生类的构造函数

     

    基类与派生类析构

    调用顺序

    1派生类的析构

    2 成员类对象析构

    3 基类析构

     

    虚成员函数(virtual)----多态

    编译期间绑定:

    指非虚成员函数调用

    调用定义类型的成员函数 

    运行期间绑定:

    指虚成员函数调用

    调用真实类型的成员函数

     

     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     //-----虚函数:子类的可能会重写父类的的方法,当父类的指针指向子类的对象,子类对象调用的是子类自己的方法--------
    45     virtual void info() const;
    46     ~Person();
    47 };
    48 
    49 class Student:public Person{
    50 private:
    51     float score;
    52 public:
    53     void info() const;
    54     Student();
    55     ~Student();
    56 };
    57 
    58 class Worker:public Person{
    59 public :
    60     void info() const;
    61 };
    62 #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     printf(" this is a person
    ");
     35 
     36 }
     37 //--------构造函数:初始化成员变量------------
     38 Person::Person(){
     39     printf("call the functon Person()
    ");
     40     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
     41     //strcpy(name, "a man");
     42     //在堆里分配内存,返回首地址,在堆里面分配的空间一定要记得释放内存!!!!!
     43     name = new char[255];
     44     strcpy(name, "a man");
     45     weight=60;
     46     age=20;
     47     sex='m';
     48 }
     49 //--------构造函数:重载------------
     50 Person::Person(int age){
     51     printf("call the functon Person(int age)
    ");
     52     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
     53     //strcpy(name, "a man");
     54     name = new char[255];
     55     strcpy(name, "a man");
     56     weight=60;
     57     this->age=age;
     58     sex='m';
     59     
     60 }
     61 
     62 //------拷贝构造函数-------
     63 Person::Person(const Person & person){
     64 /* 自己不实现的拷贝构造函数的话,系统生成的拷贝构造函数
     65     this->name=person.name;
     66     this->age=person.age;
     67     this->weight=person.weight;
     68     this->sex=person.sex;
     69 */
     70  // 自己实现的拷贝构造函数的话
     71     //重新分配内存
     72     this->name= new char[255];
     73     strcpy(this->name, person.name);
     74     this->age=person.age;
     75     this->weight=person.weight;
     76     this->sex=person.sex;
     77     
     78 }
     79 
     80 //-----------析构函数---------
     81 Person::~Person(){
     82     //在析构函数里面释放内存
     83     printf("call the functon ~Person()析构函数 
    ");
     84     delete [] name;
     85    
     86 }
     87 
     88 //----------操作符重载----------
     89 Person & Person::operator=(const Person &right){
     90     /* 自己不实现的操作符重载的话,系统生成的拷贝构造函数
     91     
     92     this->name=right.name;
     93     this->age=right.age;
     94     this->sex=right.sex;
     95     this->weight=right.weight;
     96       */
     97     //---------不需要再次分配内存-------
     98    // this->name= new char[255];
     99     strcpy(this->name, right.name);
    100     this->age=right.age;
    101     this->sex=right.sex;
    102     this->weight=right.weight;
    103     return *this;
    104 }
    105 
    106 //--------------student----------
    107 //-------------函数名字一样:函数覆盖----------------
    108 void Student::info() const{
    109    // printf("%s
    %d
    %c
    %d %.2f
    ",name,age,sex,weight,score);
    110    // Person::info();
    111     //printf("%.2f
    ",score);
    112     printf(" this is a student
    ");
    113 
    114 }
    115 //---------子类的构造函数:不需要再调用父类的构造函数------
    116 Student::Student(){
    117     score=1.00f;
    118 
    119 }
    120 Student::~Student(){
    121     printf(" call the functon ~Student()析构函数 
    ");
    122 
    123 }
    124 
    125 void Worker::info() const{
    126     printf(" this is a worker
    ");
    127 
    128 }
     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 
    14 void sayhi(Person * per){
    15     per->info();
    16 }
    17 int main()
    18 {
    19     
    20     Student stu= Student();
    21     Worker wor= Worker();
    22     
    23     stu.info();
    24     wor.info();
    25     
    26     //--如果不设置为虚函数,输出的是per的info;设置虚函数以后,调用的是stu的info-----
    27     sayhi(&stu);
    28     Person *per=&stu;
    29     per->info();
    30     
    31     sayhi(&wor);
    32 }

    输出:

    call the functon Person()

    call the functon Person()

     this is a student

     this is a worker

     this is a student

     this is a student

     this is a worker

    call the functon ~Person()析构函数 

     call the functon ~Student()析构函数 

    call the functon ~Person()析构函数 

  • 相关阅读:
    自定义Collection类
    基本排序算法(冒泡排序,选择排序,插入排序)
    泛型
    XSD的学习
    SSH整合配置
    一个可以随时插入的json的简单实现
    将Properties文件的键值对调换位置重新生成一个文件
    JAVA MD5加密
    框架中退出登录
    java 生成 xml
  • 原文地址:https://www.cnblogs.com/aosting/p/3513690.html
Copyright © 2011-2022 走看看