C++上机实验二:派生类的设计与使用

简介:
//////////////////////////////////////////////////////////////
//--------------------declaration of class CEmployee --------

#ifndef _CEMPLOYEE_H_ 
#define _CEMPLOYEE_H_

class CEmployee
{
private:
	char *m_pName;   //name
	int  m_nAge;     //age
	float m_fSalary; // salary
	char *m_pDepartment; //deparment
public:
	//-----------constructor's declaration---------------------
	CEmployee(char *pName = "", char *pDepartment = "", int age = 0, float salary = 0.0);

	CEmployee(const CEmployee &src);


	//-----------destructor's declaration----------------------
	~CEmployee();

	//-----------others's member functions's declaration-------
	void SetName(char *name);    //set the employee's name

        char *GetName() const;       //retrieve the employee's name

        void SetAge(int age);        //set the employee's age

	int GetAge() const;          //retrieve the employee's age

	void SetSalary(float salary);//set the employee's salary

	float GetSalary() const;     //retrieve the employee's salary

	void SetDepartment(char *department); //set the employee's department

	char *GetDepartment() const; //retrieve the employee's department

	void Print() const;          //output the information
};

#endif 




/////////////////////////////////////////////////////////////
//------------Declaration of class CManager------------------
#include "CEmployee.h"

/////////////////////////////////////////////////////////////
#ifndef _CMANAGER_H_
#define _CMANAGER_H_

class CManager : public CEmployee
{
	//-------------private members' declaration-------------
private:
	int m_nLevel;      //rank

	//-------------public member functions' declaration------
public:

	//-------------default constructor -----------------------
	CManager(char *name = "", char *department = "", int age = 0,
		     float salary = 0.0, int level = 0);
    
	//-------------copy constructor---------------------------
        CManager(const CManager &m);

	//-------------destructor declaration----------------------
	~CManager();

	//-------------other member functions ------------------------
	void SetLevel(int level);         //set the level

	int  GetLevel() const;            //get the level

	void Print() const;               //output the information

};

#endif
////////////////////////////////////////////////////////////////




#include <iostream>
#include <cstring>
using namespace std;

#include "CEmployee.h"
#include "CManager.h"

//-------------------------------------------------------------------------------
CManager::CManager(char *name, char *department, int age, float salary, int level) 
        : CEmployee(name, department, age, salary)
{
	cout << "Called CManager constructor "<< endl; 
	m_nLevel = level;
}

//-------------------------------------------------------------------------------
CManager::CManager(const CManager &src) : CEmployee(src)
{
	cout << "Called CManager constructor "<< endl; 
	m_nLevel = src.m_nLevel;	
}

//-------------------------------------------------------------------------------
CManager::~CManager()
{
	cout << "Called ~CManager destructor " << endl;
	//Nothing to do
}

//-------------------------------------------------------------------------------
inline void CManager::SetLevel(int level)
{
	m_nLevel = level;
}

//-------------------------------------------------------------------------------
int CManager::GetLevel() const
{
	return m_nLevel;
}
//-------------------------------------------------------------------------------

void CManager::Print() const
{
	CEmployee::Print();
	cout << "Level : " << m_nLevel << endl;
}




#include <iostream>
#include <cstring>
using namespace std;

#include "CEmployee.h"

//-----------------------------------------------------------------------
CEmployee::CEmployee(char *pName, char *pDepartment, int age, float salary)
{
	int nLenOfName = strlen(pName);
	int nLenOfDepart = strlen(pDepartment);

	m_pName = new char [nLenOfName+1];
	strcpy(m_pName, pName);
        m_pName[nLenOfName] = '\0';

	m_pDepartment = new char[nLenOfDepart+1];
	strcpy(m_pDepartment, pDepartment);
	m_pDepartment[nLenOfDepart] = '\0';

	m_nAge = age;
	m_fSalary = salary;

	cout << "Called CEmployee constructor "<< endl; 
}

//------------------------------------------------------------------------
CEmployee::CEmployee(const CEmployee &src)
{
        if (!m_pName)
		delete [] m_pName;
	if (!m_pDepartment)
		delete [] m_pDepartment;

	int len1 = strlen(src.GetName());
	int len2 = strlen(src.GetDepartment());

	m_pName = new char[len1+1];
	strcpy(m_pName, src.GetName());
	m_pName[len1] = '\0';

	m_pDepartment = new char[len2+1];
	strcpy(m_pDepartment, src.GetDepartment());
	m_pDepartment[len2] = '\0';

	m_nAge = src.GetAge();
	m_fSalary = src.GetSalary();

	cout << "Called CEmployee constructor "<< endl; 
}

//-------------------------------------------------------------------------
CEmployee::~CEmployee()
{
	if (!m_pName)
		delete [] m_pName;
	if (!m_pDepartment)
		delete [] m_pDepartment;

	cout << "Called ~CEmployee() " << endl;
}

//------------------------------------------------------------------------
void CEmployee::SetName(char *name)
{
	if (!m_pName)
	    delete [] m_pName;
	int len = strlen(name);

	m_pName = new char[len+1];
	strcpy(m_pName, name);
	m_pName[len] = '\0';
}

//--------------------------------------------------------------------------
inline char * CEmployee::GetName() const 
{
	return m_pName;
}

//--------------------------------------------------------------------------
inline char * CEmployee::GetDepartment() const
{
        return m_pDepartment;
}

//--------------------------------------------------------------------------
void CEmployee::SetDepartment(char *pDepartment)
{
        if (!m_pDepartment)
	    delete [] m_pDepartment;

	int len = strlen(pDepartment);

	m_pDepartment = new char[len+1];
	strcpy(m_pDepartment, pDepartment);
	m_pDepartment[len] = '\0';
}

//--------------------------------------------------------------------------
inline void CEmployee::SetAge(int age) 
{
    m_nAge = age;
}

//--------------------------------------------------------------------------
inline int CEmployee::GetAge()  const
{
    return m_nAge;
}

//--------------------------------------------------------------------------
inline float CEmployee::GetSalary() const
{
	return m_fSalary;
}

//--------------------------------------------------------------------------
inline void CEmployee::SetSalary(float salary)
{
    m_fSalary = salary;
}

//--------------------------------------------------------------------------
void CEmployee::Print() const
{
    cout << "Name : " << m_pName << endl
		 << "Department : " << m_pDepartment << endl
		 << "Age : " << m_nAge << endl
		 << "Salary : " << m_fSalary << endl;
}
//--------------------------------------------------------------------------




#include <iostream>
#include <cstdlib>
using namespace std;

#include "CEmployee.h"
#include "CManager.h"

int main()
{
	CEmployee e1("huang", "economic", 22, 12000.0);
        cout << "--------------------Test the class CEmployee-------------" << endl;
 
	cout << e1.GetName() << endl;
	cout << e1.GetDepartment() << endl;
	cout << e1.GetAge() << endl;
	cout << e1.GetSalary() << endl;
	e1.Print();
	cout << "sizeof(m_pName) = " << sizeof(e1.GetName()) << endl;
	cout << "sizeof(m_pDepartment) = " << sizeof(e1.GetDepartment()) << endl;
	cout << "sizeof(m_nAge) = " << sizeof(e1.GetAge()) << endl;
	cout << "sizeof(m_fSalary) = " << sizeof(e1.GetSalary()) << endl;
        cout << "sizeof(CEmployee) = " << sizeof(CEmployee) << endl;
    
        cout << "--------------------Test the class CManager-------------" << endl;
        CManager *pManager, manager("hecong", "economic", 22, 12000.0, 3);
	pManager = &manager;
	cout << pManager->GetName() << endl;
	cout << pManager->GetDepartment() << endl;
	cout << pManager->GetAge() << endl;
	cout << pManager->GetSalary() << endl;
	cout << pManager->GetLevel() << endl;
        pManager->Print();
   
	cout << "sizeof(m_pName) = " << sizeof(manager.GetName()) << endl;
	cout << "sizeof(m_pDepartment) = " << sizeof(manager.GetDepartment()) << endl;
	cout << "sizeof(m_nAge) = " << sizeof(manager.GetAge()) << endl;
	cout << "sizeof(m_fSalary) = " << sizeof(manager.GetSalary()) << endl;
	cout << "sizeof(m_nLevel) = " << sizeof(pManager->GetLevel()) << endl;
        cout << "sizeof(CManager) = " << sizeof(CManager) << endl;
    
	system("pause");
	return 0;
}


 

目录
相关文章
|
1天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
1天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
2天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
2天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
16 0
|
5天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
7天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
7天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
|
7天前
|
存储 编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
|
8天前
|
C++
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元

热门文章

最新文章