Dripping Reverse-C ++ _ Construction \ Destruction, Inheritance

1. Constructor

                              

The characteristics of the constructor:

1. The same name as the class
2. No return value
3. Executed when the object is created
4. Mainly used for initialization
5. There can be multiple (preferably one with no parameters), called overloaded other functions can also be overloaded
6 . The compiler does not require that you must provide

2. Destructor

Characteristics of destructor:

1. There can only be one destructor, not overload
2. Can not take any parameters
3. Can not take the return value
4. Mainly used for cleanup work
5. The compiler does not require it

3. Inheritance

1. Inheritance is the replication of data
2. The role of inheritance: reduce the writing of repeated code
3. Person is called the parent or base class
4. Teacher, Student is called the subclass or derived class
5. T and s can be called objects or Example.
6. You can use the pointer of the parent class to point to the object of the child class.

              

        

Multi-level inheritance:

Multiple inheritance:

Note: The constructor method is used to initialize the object of the class. Unlike other members of the parent class, it cannot be inherited by the subclass (the subclass can inherit all the member variables and member methods of the parent class, but does not inherit the parent class constructor). Therefore, when creating a subclass object, in order to initialize the data members inherited from the parent class, the system needs to call the construction method of its parent class.

4. Exercises

Exercise 1

#include"stdio.h"
#include<Windows.h>

struct DateInfo
{
	int year;
	int month;
	int day;

	DateInfo(int year,int month,int day)
	{
		this->year = year;
		this->month = month;
		this->day = day;
	}
	
	DateInfo()
	{
		this->year = 2015;
		this->month = 4;
		this->day = 2;
	}
	

	void SetDay(int day)
	{
		this->day = day;
	}

	int GetDay()
	{
		return day;
	}

	void SetYear(int year)
	{
		this->year = year;
	}

	int GetYear()
	{
		return year;
	}

	void SetMonth(int month)
	{
		this->month = month;
	}

    int GetMonth()
	{
		return month;
	}
};

struct TimeInfo:DateInfo
{
	int hour;
	int minute;
	int second;

	void SetHour(int hour)
	{
		this->hour = hour;
	}

	int GetHour()
	{
		return hour;
	}
	void SetMinute(int minute)
	{
		this->minute = minute;
	}

	int GetMinute()
	{
		return minute;
	}
	void SetSecond(int second)
	{
		this->second = second;
	}

	int GetSecond()
	{
		return second;
	}

};


void Test()
{
	TimeInfo time;
	time.SetHour(12);
	time.SetMinute(23);
	time.SetSecond(34);

	TimeInfo* p1 = &time;
	
	printf("%d %d %d %d %d %d\n", p1->GetYear(), p1->GetMonth(), p1->GetDay(), p1->GetHour(), p1->GetMinute(), p1->GetSecond());

	DateInfo* p2 = &time;

	printf("%d %d %d %d %d %d\n", p2->GetYear(), p2->GetMonth(), p2->GetDay());	
}

int main()
{
	Test();
	return 0;
}

Exercise 2

#include"stdio.h"
#include<Windows.h>

struct MyString
{
	char *arr;
	MyString(int n)
	{
		arr = (char*)malloc(n);
	}
	MyString()
	{
		arr = (char*)malloc(1024);
	}
	~MyString()
	{
		printf("析构函数执行了");
		free(arr);
		arr = NULL;
	}
	void SetString(char* str)
	{
		strcpy(arr, str);
	}
	void PrintString()
	{
		puts(arr);
	}
	void AppendString(char* str)
	{
		strcat(arr, str);
	}
	int Size()
	{
		return strlen(arr);
	}
};

void test()
{
	char arr1[] = "sunr.";
	char arr2[] = "~";

	MyString MyStr;

	MyString* p = &MyStr;

	p->SetString(arr1);

	p->PrintString();
	
	printf("Size: %d\n", p->Size());

	p->AppendString(arr2);

	p->PrintString();

	printf("Size: %d\n", p->Size());

}

int main()
{
	test();

	return 0;
}

Published 20 original articles · Likes2 · Visits 633

Guess you like

Origin blog.csdn.net/z17805008775/article/details/105628979