【CPPTraining】Techniques towards Less Coding (Cont.) PA

来源:互联网 发布:剑灵数据图怎么导入 编辑:程序博客网 时间:2024/04/30 05:49

      培训内容简要回顾

      1)组成与继承

            a)宏观上的认识

            b)类的概念的回顾

            c)构造函数的初始化列表

            d)组成对象

            e)继承对象

      2)多态与虚函数

            a)宏观上的认识

            b)多态概念的引出

            c)继承时使用虚函数

            d)纯虚函数与抽象类

      3)利用模板来编程

            a)宏观上的认识

            b)模板的概念

            c)函数模板

            d)类模板

            e)模板的特化

            f)模板的非类型参数

            g)关于模板的其他探讨

      参考资料(Links)

      1)组成与继承

      2)多态与虚函数

      3)模板简介

      编程练习题目



      对象间关系示意图


      一种实现的方式

      lab.hpp

#ifndef _LAB_HPP_#define _LAB_HPP_#include <iostream>#include <vector>#include <string>class Person{protected:std::string m_szName;public:Person(){std::cout << "A person is created" << std::endl;}Person( const std::string& szName ) : m_szName( szName ){std::cout << "A person named " << m_szName << " is created" << std::endl;}virtual ~Person(){std::cout << "A person named " << m_szName << " is destroyed" << std::endl;}};class Teacher : public Person{public:Teacher(){}Teacher( const std::string& szName ) : Person( szName ){}virtual ~Teacher(){}};class Professor : public Teacher{public:Professor(){}Professor( const std::string& szName ) : Teacher( szName ){}~Professor(){}};class AccociateProfessor : public Teacher{public:AccociateProfessor(){}AccociateProfessor( const std::string& szName ) : Teacher( szName ){}~AccociateProfessor(){}};class Lecturer : public Teacher{public:Lecturer(){}Lecturer( const std::string& szName ) : Teacher( szName ){}~Lecturer(){}};class Student : public Person{public:Student(){}Student( const std::string& szName ) : Person( szName ){}virtual ~Student(){}};class UnderGraduateStudent : public Student{public:UnderGraduateStudent(){}UnderGraduateStudent( const std::string& szName ) : Student( szName ){}~UnderGraduateStudent(){}};class MasterStudent : public Student{public:MasterStudent(){}MasterStudent( const std::string& szName ) : Student( szName ){}~MasterStudent(){}};class PhDStudent : public Student{public:PhDStudent(){}PhDStudent( const std::string& szName ) : Student( szName ){}~PhDStudent(){}};class Group{protected:std::string m_szName;std::vector<Person*> m_vecPeople;public:Group(){std::cout << "A group is created" << std::endl;}Group( const std::string& szName ) : m_szName( szName ){std::cout << "A group named " << m_szName << " is created" << std::endl;}virtual ~Group(){std::cout << "A group named " << m_szName << " is destroyed" << std::endl;}public:void addPerson( Person* pPerson ){m_vecPeople.push_back( pPerson );}};class ProjectGroup : public Group{public:ProjectGroup(){}ProjectGroup( const std::string& szName ) : Group( szName ){}~ProjectGroup(){}};class ResearchGroup : public Group{public:ResearchGroup(){}ResearchGroup( const std::string& szName ) : Group( szName ){}~ResearchGroup(){}};class Laboratory{private:std::string m_szName;std::vector<Group*> m_vecGroups;public:Laboratory(){std::cout << "A laboratory is created" << std::endl;}Laboratory( const std::string& szName ) : m_szName( szName ){std::cout << "A laboratory named " << m_szName << " is created" << std::endl;}~Laboratory(){std::cout << "A laboratory named " << m_szName << " is destroyed" << std::endl;}public:void addGroup( Group* pGroup ){m_vecGroups.push_back( pGroup );}};#endif

      main.cpp

#include <iostream>#include "lab.hpp"int main(){// Create a lab named 'MC Lab'.Laboratory lab( "MC Lab." );// Create a Research group named 'Computer Vision'Group* pCV = new ResearchGroup( "Computer Vision" );// Add a MasterStudent to the 'Computer Vision' groupPerson* pRC = new MasterStudent( "RC" );pCV->addPerson( pRC );// Add the 'Computer Vision' group to the Lab.lab.addGroup( pCV );// Add the other groups to the Lab.Group* pDM = new ResearchGroup( "Data Mining" );Group* pWSN = new ResearchGroup( "Wireless Sensor Network" );Group* pZHM = new ProjectGroup( "Zhuhai Mobile" );lab.addGroup( pDM );lab.addGroup( pWSN );lab.addGroup( pZHM );// Clean up the memorydelete pCV;delete pRC;delete pDM;delete pWSN;delete pZHM;// Pause before exiting the programstd::cin.get();return 0;};

      基于上述实现程序的输出

A laboratory named MC Lab. is createdA group named Computer Vision is createdA person named RC is createdA group named Data Mining is createdA group named Wireless Sensor Network is createdA group named Zhuhai Mobile is createdA group named Computer Vision is destroyedA person named RC is destroyedA group named Data Mining is destroyedA group named Wireless Sensor Network is destroyedA group named Zhuhai Mobile is destroyedA laboratory named MC Lab. is destroyed

原创粉丝点击