Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
75 views

Classes and Objects Part 1

Classes are the central feature of C++ that supports object-oriented programming. A class specifies the form of an object and combines data representation and methods for manipulating that data. When defining a class, you define a blueprint for a data type without actually defining any data, but defining what an object of the class will consist of and what operations can be performed on it. Classes support public, private, and protected access specifiers to control access to members. Constructors are special member functions that are executed whenever new objects of a class are created.

Uploaded by

Sadiq Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Classes and Objects Part 1

Classes are the central feature of C++ that supports object-oriented programming. A class specifies the form of an object and combines data representation and methods for manipulating that data. When defining a class, you define a blueprint for a data type without actually defining any data, but defining what an object of the class will consist of and what operations can be performed on it. Classes support public, private, and protected access specifiers to control access to members. Constructors are special member functions that are executed whenever new objects of a class are created.

Uploaded by

Sadiq Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Classes and Objects [https://www.tutorialspoint.com/cplusplus/cpp_classes_objects.

htm]
The main purpose of C++ programming is to add object orientation to the C programming language and
classes are the central feature of C++ that supports object-oriented programming, also called user-defined
types.

A class specifies the form of an object and it combines data representation and methods for manipulating
that data into one neat package. The data and functions within a class are called members of the class.

C++ Class Definitions


When you define a class, you define a blueprint for a data type. This does not actually define any data, but it
does define what the class name means, that is, what an object of the class will consist of and what
operations can be performed on such an object.

A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a
pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For
example, we defined the Box data type using the keyword class as follows:

class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

The keyword public determines the access attributes of the members of the class that follows it. A public
member can be accessed from outside the class anywhere within the scope of the class object. You can also
specify the members of a class as private or protected which we will discuss in a sub-section.

Define C++ Objects


A class provides the blueprints for objects, so basically an object is created from a class. We declare objects
of a class with exactly the same sort of declaration that we declare variables of basic types. Following
statements declare two objects of class Box:

Box Box1; // Declare Box1 of type Box


Box Box2; // Declare Box2 of type Box
Both of the objects Box1 and Box2 will have their own copy of data members.

Accessing the Data Members


The public data members of objects of a class can be accessed using the direct member access operator (.).
Let us try the following example to make the things clear:

class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here

// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;

// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;

// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<'\n';

// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<'\n';
return 0;
}
Output:
Volume of Box1 : 210
Volume of Box2 : 1560
It is important to note that private and protected members can not be accessed directly using direct
member access operator (.). We will learn how private and protected members can be accessed.

Class Member Functions


A member function of a class is a function that has its definition or its prototype within the class definition
like any other variable. It operates on any object of the class of which it is a member, and has access to all
the members of a class for that object.

Let us take previously defined class to access the members of the class using a member function instead of
directly accessing them:
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void);// Returns box volume
};

Member functions can be defined within the class definition or separately using scope resolution operator:
Defining a member function within the class definition declares the function inline, even if you do not use
the inline specifier. So either you can define Volume() function as below:
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box

double getVolume(void) {
return length * breadth * height;
}
};
If you like, you can define the same function outside the class using the scope resolution operator (::) as
follows:
double Box::getVolume(void) {
return length * breadth * height;
}
Here, only important point is that you would have to use class name just before :: operator. A member
function will be called using a dot operator (.) on a object where it will manipulate data related to that
object only as follows:
Box myBox; // Create an object

myBox.getVolume(); // Call member function for the object


Let us put above concepts to set and get the value of different class members in a class:
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box

// Member functions declaration


double getVolume(void);
void setLength( double len );
void setBreadth( double bre );
void setHeight( double hei );
};

// Member functions definitions


double Box::getVolume(void) {
return length * breadth * height;
}

void Box::setLength( double len ) {


length = len;
}
void Box::setBreadth( double bre ) {
breadth = bre;
}
void Box::setHeight( double hei ) {
height = hei;
}

// Main function for the program


int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);

// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);

// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<'\n';

// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<'\n';
return 0;
}
Output:
Volume of Box1 : 210
Volume of Box2 : 1560

Class Access Modifiers


Data hiding is one of the important features of Object Oriented Programming which allows preventing the
functions of a program to access directly the internal representation of a class type. The access restriction to
the class members is specified by the labeled public, private, and protected sections within the class body.
The keywords public, private, and protected are called access specifiers.

A class can have multiple public, protected, or private labeled sections. Each section remains in effect until
either another section label or the closing right brace of the class body is seen. The default access for
members and classes is private.
class Base {
public:
// public members go here
protected:

// protected members go here


private:
// private members go here

};

The public Members


A public member is accessible from anywhere outside the class but within a program. You can set and get
the value of public variables without any member function as shown in the following example:

class Line {
public:
double length;
void setLength( double len );
double getLength( void );
};
// Member functions definitions
double Line::getLength(void) {
return length ;
}

void Line::setLength( double len) {


length = len;
}

// Main function for the program


int main() {
Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<'\n';

// set line length without member function


line.length = 10.0; // OK: because length is public
cout << "Length of line : " << line.length <<'\n';

return 0;
}
Output:
Length of line : 6
Length of line : 10

The private Members


A private member variable or function cannot be accessed, or even viewed from outside the class. Only the
class and friend functions can access private members.

By default all the members of a class would be private, for example in the following class width is a private
member, which means until you label a member, it will be assumed a private member, we define:
class Box {
double width;

public:
double length;
void setWidth( double wid );
double getWidth( void );
};
Practically data in private section and related functions in public section so that they can be called from
outside of the class as shown in the following program.
class Box {
public:
double length;
void setWidth( double wid );
double getWidth( void );

private:
double width;
};
// Member functions definitions
double Box::getWidth(void) {
return width ;
}

void Box::setWidth( double wid ) {


width = wid;
}

// Main function for the program


int main() {
Box box;

// set box length without member function


box.length = 10.0; // OK: because length is public
cout << "Length of box : " << box.length <<'\n';

// set box width without member function


// box.width = 10.0; // Error: because width is private
box.setWidth(10.0); // Use member function to set it.
cout << "Width of box : " << box.getWidth() <<'\n';

return 0;
}
Output:
Length of box : 10
Width of box : 10

Class Constructor and Destructor

The Class Constructor


A class constructor is a special member function of a class that is executed whenever we create new objects
of that class.

A constructor will have exact same name as the class and it does not have any return type at all, not even
void. Constructors can be very useful for setting initial values for certain member variables.

Following example explains the concept of constructor:


class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor
private:
double length;
};

// Member functions definitions including constructor


Line::Line(void) {
cout << "Object is being created" << '\n';
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}

// Main function for the program


int main() {
Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<'\n';

return 0;
}
Output:
Object is being created
Length of line : 6

Parameterized Constructor
A default constructor does not have any parameter, but if you need, a constructor can have parameters.
This helps you to assign initial value to an object at the time of its creation as shown in the following
example:
class Line {
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor

private:
double length;
};

// Member functions definitions including constructor


Line::Line( double len) {
cout << "Object is being created, length = " << len << '\n';
length = len;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}

// Main function for the program


int main() {
Line line(10.0);

// get initially set length.


cout << "Length of line : " << line.getLength() <<'\n';

// set line length again


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<'\n';
}
Output:
Object is being created, length = 10
Length of line : 10
Length of line : 6

Using Initialization Lists to Initialize Fields


In case of parameterized constructor, you can use following syntax to initialize the fields:

Line::Line( double len): length(len) {


cout << "Object is being created, length = " << len << '\n';
}
Above syntax is equal to the following syntax:
Line::Line( double len) {
cout << "Object is being created, length = " << len << '\n';
length = len;
}
If for a class C, you have multiple fields X, Y, Z, etc., to be initialized, then use can use same syntax and separate the
fields by comma as follows:
C::C( double a, double b, double c): X(a), Y(b), Z(c) {
....
}

The Class Destructor


A destructor is a special member function of a class that is executed whenever an object of it's class goes out
of scope or whenever the delete expression is applied to a pointer to the object of that class.

A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value
nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of
the program like closing files, releasing memories etc.

Following example explains the concept of destructor:


class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration

private:
double length;
};

// Member functions definitions including constructor


Line::Line(void) {
cout << "Object is being created" << '\n';
}
Line::~Line(void) {
cout << "Object is being deleted" << '\n';
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}

// Main function for the program


int main() {
Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<'\n';

return 0;
}
Output:
Object is being created
Length of line : 6
Object is being deleted

You might also like