zoukankan      html  css  js  c++  java
  • 实验4 继承

    实验任务2

    #include <iostream>
    #include <typeinfo>
    
    // definitation of Graph
    class Graph
    {
    public:
        virtual void draw() { std::cout << "Graph::draw() : just as an interface\n"; }
    };
    
    
    // definition of Rectangle, derived from Graph
    class Rectangle : public Graph
    {
    public:
        void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; }
    };
    
    
    // definition of Circle, derived from Graph
    class Circle : public Graph
    {
    public:
        void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; }
    };
    
    
    // definitaion of fun(): as a call interface
    void fun(Graph *ptr)
    {
        std::cout << "pointer type: " << typeid(ptr).name() << "\n";
        std::cout << "RTTI type: " << typeid(*ptr).name() << "\n";
        ptr -> draw();
    }
    
    // test 
    int main()
    {
        Graph g1;
        Rectangle r1;
        Circle c1;
    
        // call by object name
        g1.draw();
        r1.draw();
        c1.draw();
    
        std::cout << "\n";
    
        // call by object name, and using the scope resolution operator::
        r1.Graph::draw();
        c1.Graph::draw();
    
        std::cout << "\n";
    
        // call by pointer to Base class
        fun(&g1);
        fun(&r1);
        fun(&c1);
    }

     

     

    同名覆盖原则:派生类和继承类函数完全相同时,通过派生类对象只访问派生类中的函数。

    二元作用域分辨符:可以指定想要访问某个类的函数。

    类型兼容原则:派生类对象可以作为基类的对象使用。

    实验任务3

    battery.hpp

    #ifndef BATTERY_HPP
    #define BATTERY_HPP
    #include<iostream>
    using namespace std;
    class Battery{
        public:
            Battery(int c=70):capacity(c){}
            int get_capacity(){return capacity;}
        private:
            int capacity;
    };
    #endif

    car.hpp

    #ifndef CAR_HPP
    #define CAR_HPP
    #include<iostream>
    #include<string>
    using namespace std;
    class Car{
        public:
            Car(string m,string mo,int y,int od=0):maker(m),model(mo),year(y),odometres(od){}
            void info();
            void update_odometers(int odo);
            
            
        private:
            string maker;
            string model;
            int year;
            int odometres;
            
    };
    
    void Car::info(){ 
        cout<<"maker:        "<<maker<<endl;
        cout<<"model:        "<<model<<endl;
        cout<<"year:         "<<year<<endl;
        cout<<"odometres:    "<<odometres<<endl;
    }
    void Car::update_odometers(int odo){
        if(odo<odometres)
        {
            cout<<"Error updating value\n";
        }
        else
        {
            odometres=odo;
        }
        
    }
    #endif

    electricCar.hpp

    #ifndef ELECTRICCAR_HPP
    #define ELECTRICCAR_HPP
    #include "car.hpp"
    #include "battery.hpp"
    #include<iostream>
    #include<string>
    using namespace std;
    
    class ElectricCar:public Car
    {
        public:
            ElectricCar(string m,string mo,int y,int b=70):Car(m,mo,y),battery(b){}
            void info();
        private:
            Battery battery;
    };
    void ElectricCar::info(){
        Car::info();
        cout<<"capacity:     "<<battery.get_capacity()<<"-kWh"<<endl;
    } 
    #endif

    task3.cpp

    #include <iostream>
    #include "electricCar.hpp"
    
    int main()
    {
        using namespace std;
    
        // test class of Car
        Car oldcar("Hongqi", "h6", 2020);
        cout << "--------oldcar's info--------" << endl;
        oldcar.update_odometers(25009);
        oldcar.info();
    
        cout << endl;
    
        // test class of ElectricCar
        ElectricCar newcar("Rongwei", "i5", 2021);
        newcar.update_odometers(1000);
        cout << "\n--------newcar's info--------\n";
        newcar.info();
    }

     

    实验任务4

     pets.hpp

    #ifndef PETS_HPP
    #define PETS_HPP
    #include<iostream>
    #include<string>
    using namespace std;
    class MachinePets{
        public:
            MachinePets(const string s):nickname(s){}
            virtual string talk(){}
            string get_nickname(){
                return nickname;
            }
        private:
            string nickname;    
    };
    class PetCats: public MachinePets{
        public:
            PetCats(const string s):MachinePets(s){}
            string talk(){
                return "miao wu~";
            }
    };
    class PetDogs: public MachinePets{
        public:
            PetDogs(const string s):MachinePets(s){}
            string talk(){
                return "wang wang~";
            }
    };
    #endif

    task4.cpp

    #include <iostream>
    #include "pets.hpp"
    
    void play(MachinePets *ptr)
    {
        std::cout << ptr->get_nickname() << " says " << ptr->talk() << std::endl;
    }
    
    int main()
    {
        PetCats cat("miku");
        PetDogs dog("da huang");
    
        play(&cat);
        play(&dog);
    }

     

     

  • 相关阅读:
    读书笔记 effective c++ Item 32 确保public继承建立“is-a”模型
    读书笔记 effective c++ Item 31 把文件之间的编译依赖降到最低
    读书笔记 effective c++ Item 30 理解内联的里里外外 (大师入场啦)
    程序猿开发语言投票
    读书笔记 effective c++ Item 29 为异常安全的代码而努力
    读书笔记 effective c++ Item 28 不要返回指向对象内部数据(internals)的句柄(handles)
    C++ 11和C++98相比有哪些新特性
    读书笔记 effective c++ Item 27 尽量少使用转型(casting)
    如何一步一步用DDD设计一个电商网站(七)—— 实现售价上下文
    如何一步一步用DDD设计一个电商网站(六)—— 给购物车加点料,集成售价上下文
  • 原文地址:https://www.cnblogs.com/qiansen/p/15611973.html
Copyright © 2011-2022 走看看