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

    纯虚成员函数(virtual)

    纯虚函数只包含借口的声明,由子类负责实现

    类如:

    virtual void info () const=0;

    包涵纯虚函数的类称为抽象类,抽象类不可实例化。

    如果派生类继承了抽象类,而不实现纯虚函数的话,仍然是抽象类,不能实例化,

    只有实现了所有的纯虚函数,派生类才可以实例化。

     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     //-----纯虚函数--------
    47     virtual void info() const=0;
    48     ~Person();
    49 };
    50 
    51 class Student:public Person{
    52 private:
    53     float score;
    54 public:
    55    // void info() const;
    56     Student();
    57     ~Student();
    58 };
    59 
    60 class Worker:public Person{
    61 public :
    62     void info() const;
    63 };
    64 #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     //---person类现在是抽象类,不能实例化-------
    20     //Person per=Person();
    21     
    22     //----如果student继承了person,而不实现纯虚函数的话,仍然是抽象类,不能实例化------
    23     //Student stu = Student();
    24     
    25     Worker wor= Worker();
    26     
    27 }

    输出:

    call the functon Person()

    call the functon ~Person()析构函数 

  • 相关阅读:
    PHP-xml-1
    BZOJ 1833 ZJOI2010 count 数字计数 数位DP
    运算符总结
    大写和小写转换
    Java基础:泛型
    Codeforces Round #156 (Div. 2)---A. Greg&#39;s Workout
    【Git】GitHub for Windows使用(2) 分支
    【Git】GitHub for Windows使用(1)
    【java JVM】JVM中类的加载,加载class文件的原理机制
    【FTP】org.apache.commons.net.ftp.FTPClient实现复杂的上传下载,操作目录,处理编码
  • 原文地址:https://www.cnblogs.com/aosting/p/3513719.html
Copyright © 2011-2022 走看看