Addition to Classes in C++
| |
The data types we accept activated so far to our variables were acclimated to analyze
alone items. To make added avant-garde and complete objects, C++ allows you
to accumulation these identifiers and make a anew authentic object.
An object, such as a CD Player, a printer, a car, etc, is congenital from accumulating
assorted parts. In the aforementioned way, C++ allows you to accumulation assorted variables and
make a new item alleged a class.
What is a Class?
A chic is an orgnisation of data and functions which accomplish on them. Data
structures are alleged data associates and the functions are alleged affiliate functions,
The aggregate of data associates and affiliate functions aggregate a data item
or artlessly an object.
Imagine a aggregation that articles shoe boxes hires you to address a program that would advice architecture and analyze those shoe boxes. A shoe box is accustomed for its ambit (length, width, height), color, and shoe admeasurement that a accurate box can contain, etc. The variables that characterize such an item could be:
double Length, Width, Height;
char Color;
float ShoeSize;
A Chic Example
class ShoeBox
{
bifold Length, Width, Height;
burn Color[12];
float ShoeSize;
};The items that compose a chic are alleged associates of the class.
And the program that defines a shoe box item could be:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// Ascertain the characteristics of a shoe box
// The afterward characteristics are Absolutely random
double Length(12.55), Width(6.32), Height(8.74);
string Color("Yellow Stone");
float ShoeSize = 10.50;
// Affectation the characteristics of the shoe box
cout << "Characteristics of this shoe box";
cout << setiosflags(ios::fixed) << setprecision(2);
cout << "
Length = " << Breadth << "
Width = " << Width
<< "
Height = " << Acme << "
Volume = " << Breadth * Amplitude * Height
<< "
Color = " << Blush << "
Size = " << ShoeSize << "
";
return 0;
}
The program would produce:
Characteristics of this shoe box
Length = 12.55
Width = 6.32
Height = 8.74
Volume = 693.22
Color = Chicken Stone
Size = 10.50
Press any key to continue...
Unless ambidextrous with one shoe box, this program would be abecedarian to run for anniversary object. The band-aid is to make an item alleged box that groups aggregate that characterizes the object.
Discuss this tutorial here
Forum: Classes (Total 2 Messages) | |||
Also see ...
Constructors in C++
What is a Constructor?/h1PA architect is a appropriate adjustment that is created if the item is created or defined. This accurate adjustment holds the aforementioned name as that of the item and it initializes the instance of the item whenever that item is cre
Barring Administration
What is an Exception?/H2Exceptions are the way of abatement abrupt altitude or errors that accept occured in C++ program. h2Exception Mechanism/h2so far we accept handled absurdity altitude by using the if account to analysis some expressi
Operators Overloading
What is Abettor Overloading/H2Operator Overloading enables us to create the accepted operators, like +, , * etc, to plan with the altar of our own data types. So what we do is, address a action which redefines a accurate abettor so that it performs a specific
Destructors in C++
What is a Desctructor? /h1As against to a constructor, a destructor is alleged if a program has accomplished using an instance of an object. A destructor does the charwoman abaft the scenes. Like the absence constructor, the compiler consistently make a absenc
C++ Anamnesis Administration
C++ Anamnesis Management/H2PBy George Belotsky/A /PPPublished on Linux DevCenter (http://www.linuxdevcenter.com//A)BRhttp://www.linuxdevcenter.com/pub/a/linux/2003/05/08/cpp_mm 1.html/PPEveryone knows that anamnesis administration is a difficul
Addendum on Programming in C++
Introduction/h2Kernighan and Plaugers IThe Elements of Programming Appearance /Iwas an important and accurately affecting book. But sometimes I feel its abridged rules were taken as a cookbook access to acceptable appearance instead of the blunt announceme
|
Forum: Classes (Total 2 Messages)