Simple use of classes and objects (constructor initialization)

Source: Internet
Author: User

///simple use of classes and objects#include<iostream>using namespacestd;///overloading of constructors, calculating volumeclassBox { Public: Box (); ///constructor with no arguments to initialize theBox (intHintWintL): Height (h), Width (w), Length (l) {};///initializing with parameter list    intVolume ();///Find VolumePrivate:    intheight; intwidth; intlength;}; Box::box () {Height=Ten; Width=Ten; Length=Ten;}intBox::volume () {return  This->height* This->width* This-length;}intMain () {Box box1; Box Box2 (Ten, One, A); cout<<box1.volume () <<Endl; cout<<box2.volume () <<Endl; return 0;}
View Code

///simple use of classes and objects#include<iostream>using namespacestd;///constructors that use default parameters, which are equivalent to multiple constructorsclassBox { Public: Box (intH=Ten,intw=Ten,intL=Ten): Height (h), Width (w), Length (l) {};///initializing with parameter list    intVolume ();///Find VolumePrivate:    intheight; intwidth; intlength;};intBox::volume () {return  This->height* This->width* This-length;}intMain () {Box box1; Box Box2 (Ten, One, A); cout<<box1.volume () <<Endl; cout<<box2.volume () <<Endl; return 0;}
View Code

///simple use of classes and objects#include<iostream>using namespacestd;///initialization of an array of objectsclassBox { Public: Box (intH=Ten,intw=Ten,intL=Ten): Height (h), Width (w), Length (l) {};///initializing with parameter list    intVolume ();///Find VolumePrivate:    intheight; intwidth; intlength;};intBox::volume () {return  This->height* This->width* This-length;}intMain () {Box box[4]={Box (Ten,Ten,Ten), Box (Ten, One,Ten), Box (Ten, One, A), Box ( One, A, -),    }; cout<<box[0].volume () <<Endl; cout<<box[1].volume () <<Endl; cout<<box[2].volume () <<Endl; cout<<box[3].volume () <<Endl; return 0;}
View Code

Simple use of classes and objects (constructor initialization)

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.