实验四 继承
作者:互联网
task2
//origin
#include<iostream> #include<typeinfo> using namespace std; //definition og Graph class Graph { public: //声明时加入关键词virtual void draw() { cout << "Graph::draw(): just as an interface\n"; }; }; //definition of Rectangle, derived from Graph class Rectangle :public Graph { public: void draw() { cout << "Rectangle::draw(): programs of draws a rectangle\n"; } }; //definition of Circle, derived from Graph class Circle :public Graph { public: void draw() { cout << "Circle::draw() : programs of draws a rectangle\n"; } }; //definition of fun():as a call interface void fun(Graph* ptr) { //typeid:用于获取运行时类型信息 cout << "Pointer type: " << typeid(ptr).name() << "\n"; //Runtime Type Info的首字母缩写,运行时类型信息。 cout << "RTTI typde: " << 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(); cout << endl; //call by object name, and using the scope resolution operator:: r1.Graph::draw(); c1.Graph::draw(); cout << endl; //call by pointer to Base class fun(&g1); fun(&r1); fun(&c1); }
//after
同名覆盖原则: 不同作用域声明的标识符的可见性原则:
如果存在两个或多个具有包含关系的作用域,外层声明了一个标识符,而内层没有再次声明同名标识符,那么外层标识符在内层依然可见,如果在内层声明了同名标识符,则外层标识符在内层不可见,
称内层标识符隐藏了外层同名标识符,这种现象称为隐藏规则。
二元作用域分辨符: 作用域运算符(::)作用: (1)存在具有相同名称的局部变量时,访问全局变量。 (2)在类之外定义类相关函数。(3)访问类的静态变量。
(4)在多重继承的情况下,如果两个基类中存在相同的变量名,可以使用作用域运算符来进行区分。
(5)限定成员函数所属的类。 类型兼容原则 (1) 派生类的对象可以赋值给基类对象
(2) 派生类的对象可以初始化基类的引用。
(3) 派生类对象的地址可以赋值给基类的指针。
task 3
//battery.hpp
#ifndef BATTERY_HPP #define BATTERY_HPP #include<iostream> #include<string> using namespace std; class Battery { public: Battery(string cap = "70-kWh") :capacity(cap) {} string get_capacity(){ return capacity; } private: string capacity; }; #endif
//car.hpp
#ifndef CAR_HPP #define CAR_HPP #include<iostream> using namespace std; class Car { public: Car(string a, string b, int c, int d = 0) :maker(a), model(b), year(c), odometers(d) {} void info() { cout << "maker: " << maker << endl << "model: " << model << endl << "year: " << year << endl << "odometers: " << odometers << endl; } void update_odometers(int n) { if (n < odometers) { printf("数值有误!\n"); return; } else { odometers = n; } } private: string maker; string model; int year; int odometers; }; #endif
//electricCar.hpp
#ifndef ELECTRIC_HPP #define ELECTRIC_HPP #include<iostream> #include<string> #include"battery.hpp" #include"car.hpp" using namespace std; class ElectricCar :public Car{ public: ElectricCar(string maker, string model, int year, int odo = 0, string b = "70-kMh") : Car(maker, model, year, odo), battery(b) {} void info() { Car::info(); cout << battery.get_capacity() << endl; } private: Battery battery; }; #endif
//main.cpp
#include<iostream> #include"electricCar.hpp" int main() { using namespace std; //test class of Car Car oldcar("Audi", "a4", 2016); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(25000); oldcar.info(); cout << endl; //test class of ElectricCar ElectricCar newcar("Tesla", "model s", 2016,250,"250-kMh"); newcar.update_odometers(25000); cout << "\n--------newcar's info--------\n"; newcar.info(); }
task 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) {}; string get_nickname() { return nickname; } virtual string talk() { return "sdfaf"; } private: string nickname; }; class PetCats :public MachinePets { public: PetCats(const string n) :MachinePets(n) {} string talk() { return "miao wu~ miao wu~"; } }; class PetDogs :public MachinePets { public: PetDogs(const string n) :MachinePets(n) {} string talk() { return "wang~ wang~"; } }; #endif
//main.cpp
#include<iostream> #include"pets.hpp" void play(MachinePets* ptr) { std::cout << ptr->get_nickname() << " says " << ptr->talk() << std::endl; } int main() { PetCats cat("duo duo"); PetDogs dog("da huang"); play(&cat); play(&dog); }
总结:
1. 类的继承:
构造函数直接写继承的类的名字
2. 类的组合:
在数据成员中增加别的类的类型的成员
3. 同名:
(1)如果派生类中声明了与基类成员函数同名的新函数,即使函数的参数表不同,从基类继承的同名函数的所有重载形式也会被隐藏。
(2)如果派生类的多个基类拥有同名的成员,同时,派生类又新增这样的同名成员,派生类将隐藏所有基类的同名成员。
唯一标识和访问派生类新增成员:
对象名.派生类 或 对象指针名->成员名
使用继承的类的同名函数 继承的类的名字::函数名
4.VS
VS中虚函数有返回值,则需要返回相应类型的一个值
标签:基类,string,继承,HPP,实验,派生类,include,public 来源: https://www.cnblogs.com/gujia/p/15601045.html