C++上机实验二:派生类的设计与使用-阿里云开发者社区

开发者社区> hybcoder> 正文

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;
}


 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Lua程序设计(四)面向对象类继承
1.类继承  ①代码 Sharp = { _val = 1} --① 父类 function Sharp:new() local new_sharp = { } self.__index = self --②,self == Sharp setmetatable(n...
679 0
带你读《计算机组成与体系结构:性能设计(英文版·原书第10版)》之二:Performance Issues
本书以Intel x86体系结构和ARM两个处理器系列为例,将当代计算机系统性能设计问题与计算机组成的基本概念和原理紧密联系起来,介绍了当代计算机体系结构的主流技术和最新技术。本书作者曾13次获a得美国教材和学术专著作者协会颁发的年度最佳计算机科学教材奖。目前,他是一名独立顾问,为众多计算机和网络制造商、软件开发公司以及政府前沿研究机构提供服务。
630 0
Lua程序设计(三)面向对象实现一个简单的类
1.Lua面向对象实现步骤 ①创建一个全局表(称之为元表) ②设置这个元表的__index值(值通常为元表自己,这样就能通过__index查找到对应的属性和方法)__index 赋值其实是一个function的语法糖,Sharp.
931 0
如何设计一个RPC框架?
如何设计一个RPC框架?
6 0
IBM RSA (IBM rational software architect ) V8 学习之六 C++类模板设计
原文http://blog.csdn.net/ztz0223/article/details/7603041   前面已经讲述过了IBM RSA (IBM rational software architect ) V8(具体版本V8.
828 0
+关注
hybcoder
运营小帮手新闻推广软文发布推广平台,官网:https://www.yunyingxbs.com
382
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载