Use of this pointer, constant, static member, and friend Functions

Source: Internet
Author: User

1. Purpose and requirements of the experiment:

(1) master the explicit method of using the this pointer

(2) understand the significance and usage of static data members

(3) understanding the meaning and usage of constant data members and constant member functions

(4) Measure the test taker's knowledge about how to use functions and meta-classes.

2. experiment content:

(1) define the following classes. The class member function copy is used to copy two objects to each other. Complete the implementation of this function. (There are two ways to avoid this pointer and use this pointer)


[Cpp]
Include <iostream>
Using namespace std;
Class Myclass
{
Public:
Myclass (int a, int B)
{
X =;
Y = B;
}
Void copy (Myclass & my );
Void print ()
{
Cout <"x =" <x <"";
Cout <"y =" <y <endl;
}
Private:
Int x, y;
};
Void Myclass: copy (Myclass & my)
{
This-> x = my. x;
This-> y = my. y;
}
/* Void Myclass: copy (Myclass & my)
{
X = my. x;
Y = my. y;
}
*/
Int main ()
{
Myclass my (10, 20), t (30, 40 );
My. print ();
My. copy (t );
My. print ();
Return 0;
}

# Include <iostream>
Using namespace std;
Class Myclass
{
Public:
Myclass (int a, int B)
{
X =;
Y = B;
}
Void copy (Myclass & my );
Void print ()
{
Cout <"x =" <x <"";
Cout <"y =" <y <endl;
}
Private:
Int x, y;
};
Void Myclass: copy (Myclass & my)
{
This-> x = my. x;
This-> y = my. y;
}
/* Void Myclass: copy (Myclass & my)
{
X = my. x;
Y = my. y;
}
*/
Int main ()
{
Myclass my (10, 20), t (30, 40 );
My. print ();
My. copy (t );
My. print ();
Return 0;
}

 

(2) Design a class to implement the four arithmetic operations of two plural numbers. Functions that implement addition, subtraction, multiplication, division, and other functions.


[Cpp]
# Include <iostream>
Using namespace std;
Class Complex
{
Private:
Double Real;
Double Imag;
Public:
Complex ()
{
Real = 0; Imag = 0;
}
Complex (double x, double y)
{
Real = x;
Imag = y;
}
Complex (const Complex & c)
{
Real = c. Real;
Imag = c. Imag;
}
Void print ()
{
Cout <Real <"+" <Imag <"I" <endl;
}
Friend Complex operator + (const Complex c1, const Complex c2 );
Friend Complex operator-(const Complex c1, const Complex c2 );
Friend Complex operator * (const Complex c1, const Complex c2 );
Friend Complex operator/(const Complex c1, const Complex c2 );
};
Complex operator + (const Complex c1, const Complex c2)
{
Return Complex (c1.Real + c2.Real, c1.Imag + c2.Imag );
}
Complex operator-(const Complex c1, const Complex c2)
{
Return Complex (c1.Real-c2.Real, c1.Imag-c2.Imag );
}
Complex operator * (const Complex c1, const Complex c2)
{
Return Complex (c1.Real * c2.Real-c1.Imag * c2.Imag, c1.Real * c2.Imag + c2.Real * c1.Imag );
}
Complex operator/(const Complex c1, const Complex c2)
{
Double m = c2.Real * c2.Real + c2.Imag * c2.Imag;
Return Complex (c1.Real * c2.Real + c1.Imag * c2.Imag)/m, (c1.Imag * c2.Real-c1.Imag * c2.Imag)/m );
}
Int main ()
{
Complex c1 (1, 2), c2 (3, 4 );
C1.print ();
C2.print ();
Complex c3;
C3 = c1 + c2;
C3.print ();
C3 = c1-c2;
C3.print ();
C3 = c1 * c2;
C3.print ();
C3 = c1/c2;
C3.print ();
Return 0;
}

# Include <iostream>
Using namespace std;
Class Complex
{
Private:
Double Real;
Double Imag;
Public:
Complex ()
{
Real = 0; Imag = 0;
}
Complex (double x, double y)
{
Real = x;
Imag = y;
}
Complex (const Complex & c)
{
Real = c. Real;
Imag = c. Imag;
}
Void print ()
{
Cout <Real <"+" <Imag <"I" <endl;
}
Friend Complex operator + (const Complex c1, const Complex c2 );
Friend Complex operator-(const Complex c1, const Complex c2 );
Friend Complex operator * (const Complex c1, const Complex c2 );
Friend Complex operator/(const Complex c1, const Complex c2 );
};
Complex operator + (const Complex c1, const Complex c2)
{
Return Complex (c1.Real + c2.Real, c1.Imag + c2.Imag );
}
Complex operator-(const Complex c1, const Complex c2)
{
Return Complex (c1.Real-c2.Real, c1.Imag-c2.Imag );
}
Complex operator * (const Complex c1, const Complex c2)
{
Return Complex (c1.Real * c2.Real-c1.Imag * c2.Imag, c1.Real * c2.Imag + c2.Real * c1.Imag );
}
Complex operator/(const Complex c1, const Complex c2)
{
Double m = c2.Real * c2.Real + c2.Imag * c2.Imag;
Return Complex (c1.Real * c2.Real + c1.Imag * c2.Imag)/m, (c1.Imag * c2.Real-c1.Imag * c2.Imag)/m );
}
Int main ()
{
Complex c1 (1, 2), c2 (3, 4 );
C1.print ();
C2.print ();
Complex c3;
C3 = c1 + c2;
C3.print ();
C3 = c1-c2;
C3.print ();
C3 = c1 * c2;
C3.print ();
C3 = c1/c2;
C3.print ();
Return 0;
}

 


(3) analyze the following program, give the Statement of the horizontal line, write the program output result, and analyze the m_count function.

# Include <iostream>

Using namespace std;

Class {

Static int m_counter;

Public:

A ();

~ A ();

Staticvoid display ();

}

 

---------- // Initialize m_counter to 0;

A: ()

{

M_counter ++;

}

A ::~ A ()

{

M_counter --;

}

Void A: display ()

{

Cout <"There are:" <A: m_counter <"objectsof class A. \ n ";

}

 

Int main ()

{

A a1;

A a2;

A a3;

A: display ();

A1.display ();

}

 

(4) A store distributes a kind of goods. The goods are purchased in a box and sold in a box. The unit of weight is used for purchase and sale. The unit price varies with the weight of each box, therefore, the store must record the total weight and total value of the current stock. Write a program to simulate the purchase and sale of goods in the store by defining the class Carlo.

(This topic mainly describes the use of static data members, defines the price and weight of each item stored in private variables, and uses the total weight and total price of the inventory of static data members, define constructor and destructor. when defining the initialization function of a new object and deleting the object, the total weight and price of the object are subtracted from the total weight and total price)

(5) static member exercises

1) Write a Node class, declare a data member and a static member count, initiate a data member by the constructor, and add the static data member 1, in addition, the Destructor reduces the number of static data members by 1.

2) Compile an application on the basis of 1), create three objects, display their data members and static members, and analyze each object, and show their impact on static data members.

3) Modify (2) to allow static member functions to access static data members and make static data members private.

 

(6) The two classes are Integer Data Set classes and real data set combination classes. Complete the missing content. Complete other required content.

For example:

Class Intset

{Private:

Int num [3];

Public:

Intset (int x, int y, int z)

{// Add initialization content}

Void print ()

{// Print data}

};

Class floatset

{Private:

Float num [3];

Public:

Floatset (float x, float y, float z)

{

// Add initialization content

}

Void print ()

{// Print data

}

};

(1) Add another member function in Intset to copy the integer data of the object to the object in floatset. The prototype of this member function is:

Void settofloat (floatset & set); // The parameter is the copied target object.

(2) define a friend function to implement the above functions.

Complete two programs respectively.

(7) analyze the functions of the following program and complete the program in three methods (public data members, friends, and access private data members using member functions) to implement Animal members of the object.

# Include <iostream. h>

Class Animal;

Void SetValue (Animal &, int );

Void SetValue (Animal &, int, int );

 

Class Animal

{

Public:

Animal (): itsWeight (0), itsAge (0 ){}

Voidshow () {cout <itsWeight <endl <itsAge <endl ;}

Protected:

IntitsWeight;

IntitsAge;

};

Void SetValue (Animal & ta, int tw)

{

// Set the itsWeight Value

}

Void SetValue (Animal & ta, int tw, inttn)

{

// Set itsWeight and itsAge values

}

 

Int main ()

{

Animalpeppy;

SetValue (peppy, 5 );

Peppy. show ();

SetValue (peppy, 7, 9 );

Peppy. show ();

Return0;

}

(8) design an integer linked list class for Stack operations. That is, the node is always inserted at the beginning of the linked list, and the node is always removed (Deleted) at the beginning of the linked list. Class must contain data members that record the number of nodes. If the linked list is empty and the node removal operation is required, the class must provide an error message.

Write an application and obtain a random number of 100 times (ranging from 10 to 200). Each time a random number is greater than the previous one, it is put into the linked list. Otherwise, it is omitted. Then retrieve them one by one and obtain the sum.

Use the heap allocation method to generate nodes that meet the conditions one by one and insert them into the linked list. Each time a node is extracted from the linked list, the node must be deleted in time.

Do not complete the summation work in the Linked List class to make the Linked List class universal.

 

(9) design an appropriate class structure to complete polynomial addition, subtraction, and multiplication operations.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.