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


 

目录
相关文章
|
10月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
6月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
162 0
|
6月前
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
254 0
|
8月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
298 12
|
9月前
|
设计模式 安全 C++
【C++进阶】特殊类设计 && 单例模式
通过对特殊类设计和单例模式的深入探讨,我们可以更好地设计和实现复杂的C++程序。特殊类设计提高了代码的安全性和可维护性,而单例模式则确保类的唯一实例性和全局访问性。理解并掌握这些高级设计技巧,对于提升C++编程水平至关重要。
178 16
|
9月前
|
编译器 C++
类和对象(中 )C++
本文详细讲解了C++中的默认成员函数,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载和取地址运算符重载等内容。重点分析了各函数的特点、使用场景及相互关系,如构造函数的主要任务是初始化对象,而非创建空间;析构函数用于清理资源;拷贝构造与赋值运算符的区别在于前者用于创建新对象,后者用于已存在的对象赋值。同时,文章还探讨了运算符重载的规则及其应用场景,并通过实例加深理解。最后强调,若类中存在资源管理,需显式定义拷贝构造和赋值运算符以避免浅拷贝问题。
|
9月前
|
存储 编译器 C++
类和对象(上)(C++)
本篇内容主要讲解了C++中类的相关知识,包括类的定义、实例化及this指针的作用。详细说明了类的定义格式、成员函数默认为inline、访问限定符(public、protected、private)的使用规则,以及class与struct的区别。同时分析了类实例化的概念,对象大小的计算规则和内存对齐原则。最后介绍了this指针的工作机制,解释了成员函数如何通过隐含的this指针区分不同对象的数据。这些知识点帮助我们更好地理解C++中类的封装性和对象的实现原理。
|
9月前
|
安全 C++
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
477 6
|
9月前
|
编译器 C++
类和对象(下)C++
本内容主要讲解C++中的初始化列表、类型转换、静态成员、友元、内部类、匿名对象及对象拷贝时的编译器优化。初始化列表用于成员变量定义初始化,尤其对引用、const及无默认构造函数的类类型变量至关重要。类型转换中,`explicit`可禁用隐式转换。静态成员属类而非对象,受访问限定符约束。内部类是独立类,可增强封装性。匿名对象生命周期短,常用于临时场景。编译器会优化对象拷贝以提高效率。最后,鼓励大家通过重复练习提升技能!
|
10月前
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)