实验四

task 2 改动后

 1 #include<iostream>
 2 #include<typeinfo>
 3 
 4 //definition of Graph
 5 class Graph
 6 {
 7     public:
 8         void draw(){ std::cout<<"Graph::draw() : just as an interface\n";}        
 9 };
10 
11 
12 //definition of Rectangle, derived fron graph
13 class Rectangle : public Graph
14 {
15     public:
16         void draw() { std::cout<<"Rectangle::draw(): programs of draw a rectangl\n";}
17 };
18 
19 
20 //definition of ciecle, derived from Graph
21 class Circle : public Graph
22 {
23     public: 
24          virtual  void draw(){std:: cout<<"Circle::draw(): programs of draw a cicle\n";}
25 };
26 
27 
28 //definition of fun(): as a call interface
29 void fun(Graph *ptr)
30 {
31     std::cout<<"pointer type: "<<typeid(ptr).name()<<"\n";
32     std::cout<<"RTTI type: "<<typeid(*ptr).name()<<"\n";
33     ptr->draw();
34  } 
35  
36  //test
37 int main()
38 {
39     Graph g1;
40     Rectangle r1;
41     Circle c1;
42     
43        //call by object name
44        g1.draw();
45        r1.draw();
46        c1.draw();
47        
48        std::cout << "\n";
49        
50        //call by object name,and using the scope resolution opertor::
51        r1.Graph::draw();
52        c1.Graph::draw();
53        
54        std::cout<<"\n";
55        
56        //call by pointer to Base class
57        fun(&g1);
58        fun(&r1);
59        fun(&c1);
60 }
View Code

改动前截图:

 

改动后截图:

1. 同名覆盖原则:派生类与基类有相同成员时,若未强行指名,则通过派生类对象使用的是派生类的同名成员;

2.二元作用域分辨符:

当派生类新增成员隐藏了基类的同名成员,这时使用“对象名.成员名”的访问方式只能访问到派生类新增成员。对基类同名成员的访问,只能通过基类名哥作用域分辨符来实现

Car.hpp

 1 #ifndef Car_hpp
 2 #define Car_hpp
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 
 7 class Car
 8 {
 9     private:
10         string maker;
11         string model;
12         int year;
13         int odometers;
14     public:
15         Car(string maker1, string model1, int year1, int odometers1=0): maker(maker1), model(model1), year(year1), odometers(odometers1) { };
16         void info();
17         void update_odometers(int new_odometers);
18 };
19 
20 void Car::info(){
21     cout<<"maker:\t\t"<<maker<<endl;
22     cout<<"model:\t\t"<<model<<endl;
23     cout<<"year:\t\t"<<year<<endl;
24     cout<<"odometers:\t"<<odometers<<endl;
25 }
26 
27 void Car::update_odometers(int new_odometers){
28     if(new_odometers<odometers)
29     cout<<"更新数值有误"<<endl;
30     else
31     odometers=new_odometers; 
32 }
33 
34 #endif

Battery.hpp

#ifndef Battery_hpp
#define Battery_hpp
#include<iostream>
using namespace std;

class Battery{
    private:
        int capacity;
    public:
        Battery(int capa=70): capacity(capa){
        };
        int get_capacity(){
            return capacity;
        }
};

#endif

ElectricCar.hpp

 1 #ifndef ElectricCar_hpp
 2 #define ElectricCar_hpp
 3 #include<iostream>
 4 #include<string>
 5 #include"Car.hpp"
 6 #include"Battery.hpp"
 7 using namespace std;
 8 
 9 class ElectricCar: public Car{
10     private:
11         Battery battery;
12     public:
13      ElectricCar(string ma, string mo, int y, int c=70): Car(ma, mo, y), battery(c) {
14      };
15      void info();
16      ~ElectricCar(){};
17     
18 };
19 
20 void ElectricCar::info(){
21     Car::info();
22     cout<<"capacity:\t"<<battery.get_capacity()<<"-kwh"<<endl;
23 }
24 
25 #endif

task3.cpp

 1 #include<iostream>
 2 #include"ElectricCar.hpp"
 3 
 4 int main()
 5 {
 6     using namespace std;
 7     
 8     //test class of Car
 9     Car oldcar("lisa","c7",2102);
10     cout<<"--------oldcar's info----------"<<endl;
11     oldcar.update_odometers(50000);
12     oldcar.info();
13     
14     cout<<endl;
15     
16     //test class of ElectricCar
17     ElectricCar newcar("Ta","models",1032);
18     newcar.update_odometers(8273);
19     cout<<"\n----------newcar's info--------------\n";
20     newcar.info();
21 }

运行截图

 Pets.hpp

 1 #ifndef Pets_hpp
 2 #define Pets_hpp
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 
 7 class MachinePets{
 8     private:
 9         string nickname;
10     public:
11        MachinePets(const string nickname1): nickname(nickname1){
12        }
13       string get_nickname() const;
14       virtual string talk();        
15 };
16 
17 string MachinePets::get_nickname() const{
18        return nickname;
19 }
20 
21 string MachinePets::talk(){
22     return "hahahha";
23 }
24 
25 class PetCats: public MachinePets
26 {
27     private:
28         string nickname;
29     public:
30         PetCats(const string nickname1): MachinePets(nickname1){}
31         string talk()
32         {
33             return "miao~wu~";
34         }
35 };
36 
37 class PetDogs: public MachinePets
38 {
39     private:
40         string nickname;
41     public:
42         PetDogs(const string nickname1): MachinePets(nickname1){
43         }
44         string talk()
45         {
46             return "wang ~wang ~";
47         }
48 };
49 
50 #endif

task 4.cpp

 1 #include <iostream>
 2 #include "Pets.hpp"
 3 void play(MachinePets *ptr)
 4 {
 5     std::cout << ptr->get_nickname() << " says " << ptr->talk() <<std::endl;
 6 }
 7 int main()
 8 {
 9     PetCats cat("miku");
10     PetDogs dog("da huang");
11     
12     play(&cat);
13     play(&dog);
14 }

 

posted @ 2021-11-29 21:34  zoracoding  阅读(8)  评论(3编辑  收藏  举报