Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Define a tree class, including the age of members. The member function grow(int years) adds the year to the age, and age () displays the age of the tree object.
Define a tree class, including the age of members. The member function grow(int years) adds the year to the age, and age () displays the age of the tree object.
Let me briefly talk about the amount. When creating a Tree class, because there is no constructor in the class defined by the landlord, the system will automatically add a default constructor tree () to you. Therefore, it is ok to define Tree t 1 without parameters (or brackets), but the landlord did not set the constructor of Tree (int) when defining Tree t2(5), so the system will have compilation errors.

t 1 . age(); //Access the member function age of t 1, and its function is to output "The age of this tree is:";

t 1 . grow(3); //Access the member function grow of t 1 and pass in parameter 3, that is, set the age of t 1 and process its private data ages+= 3;; In fact, I really don't understand the membership function grow defined by the landlord. It takes a plastic year, and then

CIN & gt; & gt What do you mean, Nian doesn't understand?

Because there is no need to output the age value of t 1 and CIN >: > year, the sentence is output like this:

The age of this tree is:

Please enter the year: 3

The age of this tree is:

Supplementary estimation means that you want to pass in the parameters you want in the function of grow, and you must have output data to define the constructor in the class.

//# contains "StdAfx.h"

# include & ltiostream & gt

Use namespace std

Category tree

{

Public:

tree(int a){ age = a; }

Invalid growth (integer year);

void age();

Private:

int ages

};

Void tree:: Growth (integer year) {

Cout & lt& lt "Please enter the year:"

//CIN & gt; & gt year;

Age+= years old;

}

Void tree:: age ()

{

Cout & lt& lt "The age of this tree is:"

}

int main()

{

Tree t1(5);

t 1 . age();

//T2 . age();

t 1 . grow(3);

t 1 . age();

//T2 . grow(2);

//T2 . age();

System ("suspended");

Returns 0;

}

Output:

This tree is five years old.

Please enter the year: 3

This tree is eight years old.