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

      培训内容简要回顾

      1)组成与继承

            a)宏观上的认识

            b)类的概念的回顾

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

            d)组成对象

            e)继承对象

      2)多态与虚函数

            a)宏观上的认识

            b)多态概念的引出

            c)继承时使用虚函数

            d)纯虚函数与抽象类

      3)利用模板来编程

            a)宏观上的认识

            b)模板的概念

            c)函数模板

            d)类模板

            e)模板的特化

            f)模板的非类型参数

            g)关于模板的其他探讨

      参考资料(Links)

      1)组成与继承

      2)多态与虚函数

      3)模板简介

      编程练习题目

【CPPTraining】Techniques towards Less Coding (Cont.) PA_第1张图片

【CPPTraining】Techniques towards Less Coding (Cont.) PA_第2张图片

      对象间关系示意图

【CPPTraining】Techniques towards Less Coding (Cont.) PA_第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' group
	Person* 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 memory
	delete pCV;
	delete pRC;
	delete pDM;
	delete pWSN;
	delete pZHM;

	// Pause before exiting the program
	std::cin.get();

	return 0;
};

      基于上述实现程序的输出

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

你可能感兴趣的:(【CPPTraining】Techniques towards Less Coding (Cont.) PA)