C++ Classes And Objects

One of the most important features of C++ is classes and objects. Bjarne Stroustrup initially named C with classes. It is a way to group data elements and functions together.

C++ Classes

Classes are used to represent a set of similar objects. Class is a way in which we can bind data describing the entity and its associated function together.

The class declaration includes the declaration of four associated attributes.

Attributes Description
Data Members It defines the characteristics of the class.
Member Functions It includes the set of operations that can be applied to the objects of that class.
Program access level It controls member access from within the program.
Class tagname It acts as a type for the class.

The Class Definition:

Class definitions describe component members such as the data member and the member function of the class. The general form of class is :-

   
class class-name {
private:
[variable declaration;]
[function declaration;]
protected:
[variable declaration;]
[function declaration;]
public:
[variable declaration;]
[function declaration;]
};


Where the keyword 'class' specifies that it is a class. The class-name is a tag name through which a class object can be created. Public, private and protected is an access specifier that specifies whether members (attributes and methods) may be accessible outside the class or not.

In C++, there are three access specifiers:

Public - attributes and methods are accessible from anywhere.
Private - attributes and methods cannot be accessed (or viewed) from outside the class.
Protected - attributes and methods cannot be accessed from outside the class, however, they can be accessed in inherited classes.



Base class Access Specifier Private Protected Public
Public Public Protected Private
Protected Protected Protected Private
Private Not accessible(Hidden) Not accessible(Hidden) Not accessible(Hidden)


For example, we defined the class named 'LfcClass' using the keyword class as follows −



   
class LfcClass {       // The class
public:             // Access specifier
int lfcNum;        // Attribute (int variable)
string lfcString;  // Attribute (string variable)
};

C++ Objects

An Object is an instance of a Class. Once we define a class, the object of that class can be created like any other variable. Class defines the information that the object will contain.

Syntax :-

   
class-name object-list (comma seperated);

Example:-


   
LfcClass abc;          // Declare abc of type LfcClass
LfcClass xyz;          // Declare xyz of type LfcClass

For instance, consider the following code fragment:-



   
#include <bits/stdc++.h> 
using namespace std; 
class Lfc 
{ 
// Access specifier 
public: 
// Data Members 
string lfcname; 
// Member Functions() 
void printname() 
{ 
cout << "lfcname is: " << lfcname; 
} 
}; 
int main() { 
// Declare an object of class Lfc 
Lfc obj1; 
// accessing data member 
obj1.lfcname = "letsfindcourse"; 
// accessing member function 
obj1.printname(); 
return 0; 
} 

Output:-



lfcname is: letsfindcourse


Multiple Objects:

You can create multiple objects of a class:



   
#include<iostream>
using namespace std;
class Item{
int mynum;
public:
void readnum();
void printnum();
};
void Item :: readnum(){
cout<<"Enter any integer value "<< endl;
cin>>mynum;
}
void Item :: printnum(){
cout<<"The value  is :"<< mynum<< endl;
}
int main(){
Item i1,i2,i3;
i1.readnum();
i2.readnum();
i3.readnum();
i1.printnum();
i2.printnum();
i3.printnum();
return 0;
}

Output:-
   
Enter any integer value 741
Enter any integer value 852
Enter any integer value 963
The value is :741
The value is :852
The value is :963

Exercise:-

1. Which of the following is true?

A. All objects of a class share all data members of class
B. Objects of a class do not share non-static members. Every object has its own copy.
C. Objects of a class do not share codes of non-static methods, they have their own copy
D. None of the above

View Answer


2. A member function can always access the data in __________ .

A. the class of which it is member
B. the object of which it is a member
C. the public part of its class
D. the private part of its class

View Answer



Program :-

Object and Class in C++ Programming

#include <iostream>
using namespace std;
class Test
{
private:
int data1;
float data2;
public:
void insertIntegerData(int d)
{
data1 = d;
cout << "Number: " << data1;
}
float insertFloatData()
{
cout << "\nEnter data: ";
cin >> data2;
return data2;
}
};
int main()
{
Test o1, o2;
float secondDataOfObject2;
o1.insertIntegerData(12);
secondDataOfObject2 = o2.insertFloatData();
cout << "You entered " << secondDataOfObject2;
return 0;
}


Output :

Number: 12
Enter data: 23.3
You entered 23.3





Visit :


Discussion



* You must be logged in to add comment.