C ++ example Constructor

Source: Internet
Author: User

Compile an object-based program to obtain the volume and surface area of five long square columns. Bulk data members of a long column Class include length, width, and heigth. In addition:
(1) The rectangular column class needs to be defined. Five Rectangular Columns are represented by an array of objects;
(2) define the corresponding constructor to support initialization in the following main () function. The first three parameters are initialized directly (the default value of unspecified parameters is 1.0 ), 4th objects B [3] are initialized using the default constructor; 5th long-square-column values are assigned by the keyboard input instead of being initialized;
(3) output the volume and surface area of the five long square columns;

[Cpp]
# Include <iostream>
Using namespace std;
 
Class Bulk
{
Private:
Double length;
Double width;
Double height;
Public:
Bulk (double len = 1.0, double wid = 1.0, double hei = 1.0): length (len), width (wid), height (hei ){}
Void get_value ();
Double volume ();
Double surface_are ();
};
 
Void Bulk: get_value ()
{
Cout <"please input the length width and height:" <endl;
Cin> length> width> height;
}
 
Double Bulk: volume ()
{
Return length * width * height;
}
 
Double Bulk: surface_are ()
{
Return 2 * (length * width + width * height + height * length );
}
 
Int main ()
{
Bulk B [5] = {Bulk (2.3, 4.5, 6.7), Bulk (1.5, 3.4), Bulk (10.5 )};
B [4]. get_value ();
// Output the volume and surface area of the five long square columns respectively below
 
Cout <"volume of the first rectangular column:" <B [0]. volume () <'\ t' <"Area:" <B [0]. surface_are ()
<Endl
<"Volume of the second rectangular column:" <B [1]. volume () <'\ t' <"Area:" <B [1]. surface_are ()
<Endl
<"Volume of the third rectangular column:" <B [2]. volume () <'\ t' <"Area:" <B [2]. surface_are ()
<Endl
<"Volume of the fourth rectangular column:" <B [3]. volume () <'\ t' <"Area:" <B [3]. surface_are ()
<Endl
<"Volume of the fifth rectangular column:" <B [4]. volume () <'\ t' <"Area:" <B [4]. surface_are ()
<Endl;
 
Return 0;
}

# Include <iostream>
Using namespace std;

Class Bulk
{
Private:
Double length;
Double width;
Double height;
Public:
Bulk (double len = 1.0, double wid = 1.0, double hei = 1.0): length (len), width (wid), height (hei ){}
Void get_value ();
Double volume ();
Double surface_are ();
};

Void Bulk: get_value ()
{
Cout <"please input the length width and height:" <endl;
Cin> length> width> height;
}

Double Bulk: volume ()
{
Return length * width * height;
}

Double Bulk: surface_are ()
{
Return 2 * (length * width + width * height + height * length );
}

Int main ()
{
Bulk B [5] = {Bulk (2.3, 4.5, 6.7), Bulk (1.5, 3.4), Bulk (10.5 )};
B [4]. get_value ();
// Output the volume and surface area of the five long square columns respectively below

Cout <"volume of the first rectangular column:" <B [0]. volume () <'\ t' <"Area:" <B [0]. surface_are ()
<Endl
<"Volume of the second rectangular column:" <B [1]. volume () <'\ t' <"Area:" <B [1]. surface_are ()
<Endl
<"Volume of the third rectangular column:" <B [2]. volume () <'\ t' <"Area:" <B [2]. surface_are ()
<Endl
<"Volume of the fourth rectangular column:" <B [3]. volume () <'\ t' <"Area:" <B [3]. surface_are ()
<Endl
<"Volume of the fifth rectangular column:" <B [4]. volume () <'\ t' <"Area:" <B [4]. surface_are ()
<Endl;

Return 0;
}


 

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.