欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

the composition of class and object in C++

程序员文章站 2024-02-27 13:49:39
...

Thanks to the tutorial viodes on Youtube, yesterday I finally figured out how the composition of class and object works, which has confused me for a long time.

Let me explain it by analyzing a piece of code.

//print a person's name and its birthday
#include <iostream>
using namespace std;

class Birthday{
public:

	Birthday(int d,int m,int y);
	void printDate();


private:
	int day;
	int month;
	int year;

};

Birthday::Birthday(int d,int m,int y)
{
	day = d;
	month = m;
	year = y;
}

void Birthday::printDate(){
	cout << day << "/" << month << "/" << year << endl;
}

class People{
public:

	People(string x,Birthday bo);
	void printInfo();

private:
	string name;
	Birthday dateOfBirth;
};

People::People(string x,Birthday bo)
: name(x),dateOfBirth(bo)
{
}

void People::printInfo(){
	cout << name << " was born on" ;
	dateOfBirth.printDate();
}

int main(){

	Birthday birth(12,28,1996);

	People TheFirstClass("Jack",birth);

	TheFirstClass.printInfo();
}

For example,there are two classes called birthday and people respectively.

Let’s look at “birthday” first.

class Birthday{
public:

	Birthday(int d,int m,int y);
	void printDate();


private:
	int day;
	int month;
	int year;

};

Birthday::Birthday(int d,int m,int y)
{
	day = d;
	month = m;
	year = y;
}

void Birthday::printDate(){
	cout << day << "/" << month << "/" << year << endl;
}

As we all know, a person’s birthday should include day,year and month.

So we can add these properties to this class

Birthday::Birthday(int d,int m,int y)
{
	day = d;
	month = m;
	year = y;
}

And also, we need a method(function) to print the birthday information.

void Birthday::printDate(){
	cout << day << "/" << month << "/" << year << endl;
}

And then the class “People” comes.

What we need to do is to give the object of “People” a name and his/her birthday so that we can print the information.

class People{
public:

	People(string x,Birthday bo);
	void printInfo();

private:
	string name;
	Birthday dateOfBirth;
};

“x” represents the name and “Birthday bo” represents the date.

So how do we assign “x” to “name” and “Birthday bo” to “dateOfBirth”?

we should ues the member initialzer list.
Whenever you meet the situation of a class inside of another class, you need to use a member initializer list.

To use a member initializer list, don’t put it inside the body. Put it between parameters in the body.

People::People(string x,Birthday bo)
: name(x),dateOfBirth(bo)
{
}

What the code does is to set name euqal to x and set the Birthday object of “bo” equal to dateOfbirth so that this class can have a name and a birthday object that you can use.

And to print the information of a person, we need to create a function(method) to print the person’s name and his/her bnirthday.

void People::printInfo(){
	cout << name << " was born on" ;
	dateOfBirth.printDate();
}

Becuase we passed a Birthday object inside People class, now the People class has access to the function created in Birthday class.

So wu can use the dateOfbirth object in order to use the printDate() function.


Having analyzed the part of class, let’s take a look at the main()

int main(){

	Birthday birth(12,28,1996);

	People TheFirstClass("Jack",birth);

	TheFirstClass.printInfo();
}

First of all ,we need to create a Birthday object in order to “activate” the People object we create later.

When we create this object, remember that this object need three pieces of information:

Birthday(int d,int m,int y);

day, month, and year

Birthday birth(12,28,1996);

And then we should create a People object.

Remeber that we need two pieces of information,which are the name and the Birthday object we created before.

People(string x,Birthday bo);

x represents the person’s name

And the Birthday object represents his/her dateOfbirth .

People TheFirstClass("Jack",birth);

Then we use the function printInfo by using the TheFirstClass object.

And as we run the code, we can get this.

Jack was born on12/28/1996

From another angle,we can analyze the code in reverse to learn the composition.

int main(){

	Birthday birth(12,28,1996);

	People TheFirstClass("Jack",birth);

	TheFirstClass.printInfo();
}

When we run this code, the function “printInfo()” was activated

void People::printInfo(){
	cout << name << " was born on" ;
	dateOfBirth.printDate();
}
void Birthday::printDate(){
	cout << day << "/" << month << "/" << year << endl;
}

And we use the object of dateOfBirth and the function printDate().

Because the object of TheFirstClass has the properties of birth, which is also a object including the properties of day, month and year,

Birthday::Birthday(int d,int m,int y)
{
	day = d;
	month = m;
	year = y;
}

and Jack, which is the name, the function printDate could use these pieces of information.

People::People(string x,Birthday bo)
: name(x),dateOfBirth(bo)
{
}