C + + basic learning

Keywords: Attribute

Summary based on an example code

#include <iostream>
#include <string>

class Date
{
public:
	Date(unsigned int nYear , unsigned int nMonth , unsigned int nDay );
	virtual ~Date();
	void SetYear(unsigned int nYear);
	void SetMonth(unsigned int nMonth);
	void SetDay(unsigned int nDay);
	unsigned int GetYear();
	unsigned int GetMonth();
	unsigned int GetDay();

private:
	unsigned int m_Year;
	unsigned int m_Month;
	unsigned int m_Day;
};

class People
{
public:
	//A class can have multiple constructors
	People(unsigned int nNumber , bool nSex , unsigned int nYear , unsigned int nMonth , unsigned int nDay  , unsigned int nId);
	People();
	virtual ~People();
	//Copy constructor, mainly used to copy the same object
	People( const People &nObj );
	void SetNumber(unsigned int nNumber);
	void SetSex(bool nSex);
	void SetBirthday(unsigned int nYear , unsigned int nMonth , unsigned int nDay);
	void SetId(unsigned int nId);
	void Display();


private:
	unsigned int m_Number;
	bool m_Sex;
	Date *m_Birthday;
	unsigned int m_Id;
	//The meaning of the protected attribute
protected:
	//How to distinguish the use of variables and functions with the same name in the base class, derived class and external implementation
	int test;
};

class Student : public People
{
public:
	Student(unsigned int nNumber , bool nSex , unsigned int nYear , unsigned int nMonth , unsigned int nDay  , unsigned int nId);
	~Student();
	void ChangeTest(float nTest);
	void DisplayVal();
private:
	float test;
}; 
#include "test.h"

Date::Date(unsigned int nYear , unsigned int nMonth , unsigned int nDay)
{
	m_Year   = nYear;
	m_Month  = nMonth;
	m_Day    = nDay;
}

Date::~Date()
{

}
//Can the same function simplify the implementation
void Date::SetYear(unsigned int nYear)
{
	m_Year = nYear;
}
void Date::SetMonth(unsigned int nMonth)
{
	m_Month = nMonth;
}
void Date::SetDay(unsigned int nDay)
{
	m_Day = nDay;
}
unsigned int Date::GetYear()
{
	return m_Year;
}
unsigned int Date::GetMonth()
{
	return m_Month;
}
unsigned int Date::GetDay()
{
	return m_Day;
}

People::People(unsigned int nNumber , bool nSex , unsigned int nYear , unsigned int nMonth , unsigned int nDay , unsigned int nId)
{
	m_Number = nNumber;
	m_Sex = nSex;
	m_Birthday = new Date(nYear , nMonth , nDay);
	m_Id = nId;
}
People::People()
{

}
People::~People()
{
	
}
People::People( const People &nObj )
{
	m_Number = nObj.m_Number;
	m_Sex = nObj.m_Sex;
	m_Birthday = new Date(nObj.m_Birthday->GetYear() , nObj.m_Birthday->GetMonth() , nObj.m_Birthday->GetDay());
	m_Id = nObj.m_Id;
}
void People::SetNumber(unsigned int nNumber)
{
	m_Number = nNumber;
}
void People::SetSex(bool nSex)
{
	m_Sex = nSex;
}
void People::SetBirthday(unsigned int nYear , unsigned int nMonth , unsigned int nDay)
{
	m_Birthday->SetYear(nYear);
	m_Birthday->SetMonth(nMonth);
	m_Birthday->SetDay(nDay);
}
void People::SetId(unsigned int nId)
{
	m_Id = nId;
}
void People::Display()
{
	std::cout<< m_Number << " " << m_Sex << " " << m_Birthday->GetYear() << " " << m_Birthday->GetMonth() << " " << m_Birthday->GetDay() << " " << m_Id << std::endl;
}


Student::Student(unsigned int nNumber , bool nSex , unsigned int nYear , unsigned int nMonth , unsigned int nDay  , unsigned int nId)
{
	People(nNumber , nSex , nYear , nMonth , nDay , nId );
	test = 1.2;
}
//Here, if People does not declare a parameterless constructor as shown, students can only be constructed in this way
Student::Student(unsigned int nNumber , bool nSex , unsigned int nYear , unsigned int nMonth , unsigned int nDay  , unsigned int nId) : People(nNumber , nSex , nYear , nMonth , nDay , nId )
{
	
}
Student::~Student()
{

}

void Student::DisplayVal()
{
	std::cout << test << std::endl;
	std::cout << People::test << std::endl;
}
void Student::ChangeTest( float nTest )
{
	//Variable or function with the same name of base class and derived class
	//In the scope of the base class, the base class is operated except for the virtual function
	//In both the derived class and the external scope, operate on the derived class. If you want to operate on the base class, use People::test to access
	test = nTest;
	People::test = (int)nTest + 1;
}

int main()
{
	People *sjn = new People( 1015 , 1 , 1994 , 8 , 29 , 1817 );
	sjn->Display();
	People *zjp = new People( *sjn );
	zjp->SetId(1234);
	zjp->SetBirthday( 1995 , 1 , 2 );
	zjp->Display();
	Student *stu = new Student( 1015 , 1 , 1994 , 8 , 29 , 1817 );
	stu->ChangeTest(1.5);
	stu->DisplayVal();
	while(1);
	return 0;
}

1. A class can have many constructors

2. copy constructor is mainly used to duplicate an identical object.

3. The variables and function derived classes modified by the protected keyword can be accessed, but cannot be accessed externally

4. How to distinguish the use of variables with the same name in the base class, derived class and external implementation?

In the scope of the base class, the base class is operated except for the virtual function

In both the derived class and the external scope, operate on the derived class. If you want to operate on the base class, use the class name:: variable name or method name to access

5. If the inherited base class has parameters, you need to explicitly declare a parameterless constructor in the base class, so as to support any construction of the derived class. Otherwise, you must explicitly use the derived class (parameter...): the base class (parameter...) to construct the derived class

Posted by iamcaper on Sun, 03 Nov 2019 00:02:13 -0700