Experiment 5 - polymorphic classes, inheritance and derived 2

Part1: Validating Experiments

1. function in a derived class, if you are using the object name to access members, the final result is access to run every member function of a derived class;

2. If access is through a pointer then only access base1 among the member functions, because the first program in the BASE1 and no virtual functions, so base2 and derived the member function can not be covered, so the only access pointer defaults to the face member function BASE1, whereas the second uses virtual functions, the role of a virtual function by base-class pointer or reference to a derived class, the derived class with the same name access cover member function, the second program base1 virtual functions virtual, i.e. functions may be covered with this member.

Part2: validation experiments

Understand pumping

Part3: simple programming exercises

 1 #include<iostream>
 2 #include<string>
 3 #include<cstdlib>
 4 #include"MachinePets.h"
 5 #include"PetCats.h"
 6 #include"PetDogs.h"
 7 
 8 using namespace std;
 9 
10 
11 
12 void play(MachinePets *pet)
13 {
14     cout<<pet->getNickname()<<pet-> talk()<<endl;
15 }
16 
17 int main()
18 {
19     PetCats cat("miku");
20     PetDogs dog("dahuang");
21     play(&cat);
22     play(&dog);
23     
24     return 0;
25 }
 1 #ifndef MACHINEPETS_H
 2 #define MACHINEPETS_H
 3 
 4 #include<string>
 5 using namespace std;
 6 
 7 class MachinePets{
 8 public:
 9     MachinePets(const string s);
10     virtual string talk() const=0;
11     string getNickname();
12 private:
13     string nickname;
14 };
15 #endif
machinepets.h
#include"MachinePets.h"
#include<string>
using namespace std;


MachinePets::MachinePets(const string s):nickname(s){

}

string MachinePets::getNickname(){
    return nickname;
}
 1 #ifndef PETCATS_H
 2 #define PETCATS_H
 3 
 4 #include"MachinePets.h"
 5 #include<string>
 6 using namespace std;
 7 
 8 class PetCats:public MachinePets{
 9 public:
10     PetCats(const string s);
11     string talk()const;
12 };
13 #endif
petcats.h
 1 #include<iostream>
 2 #include<string>
 3 #include"PetCats.h"
 4 using namespace std;
 5 
 6 PetCats::PetCats(const string s):MachinePets(s){
 7 
 8 }
 9 string PetCats::talk()const{
10     return "says miao wu~";
11 }
petcats.cpp
 1 #include<iostream>
 2 #include<string>
 3 #include"MachinePets.h"
 4 using namespace std;
 5 #ifndef PETDOGS_H
 6 #define PETDOGS_H
 7 
 8 
 9 class PetDogs:public MachinePets{
10 public:
11     PetDogs(const string s);
12     string talk()const;
13 };
14 #endif
petdogs.h
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 #include"PetDogs.h"
 5 PetDogs::PetDogs(const string s):MachinePets(s){
 6 
 7 }
 8 string PetDogs::talk()const{
 9     return "says wang wang~";
10 }
petsdogs.cpp
 

实验结论:在写这个程序时,我刚开始一直尝试插入音乐,但是每次都没有成功,而且报错如图

后来有进行改进程序,插入一段音乐后缀为.wav,然而还是有报错,类似于上面的报错,最后还是没有成功,我就放弃了这个想法,只写了最基本的。写这个程序的方法还是蛮多的,不仅限于这一种。为了不让运行框闪现还可以加一句system("pause")的头文件cstdlib。

 

Guess you like

Origin www.cnblogs.com/mxueyyqx/p/10963449.html