CLASSES AND OBJECT IN C++
Hello guys, in last post I told you about c++ language .in this post, I am introducing you to object and classes in c++ ,it is a important part of c++ programming.
What is class?
A class is the building block that leads to object –oriented programming .the concept of class can be defined in numbers of ways. When we consider the animals like tiger, lion, deer, fox here animals itself is the class and tiger, lion, deer, fox are object of same class.
Class is a collection of variables having different data types and function. Class can be defined as it is a collection of objects of similar type is known as class .once a class has been defined, it exists till the program is running .class represents real world entities that have both type properties and associated operations.
Everything in c++ is associated with classes and objects, along with its attributes and methods .a class is used to specify 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. With the help of class, the entire set of data and code, can be made user defined .once class has been defined so we can create any numbers of its objects .objects are the variable of type class. a class is an extension of the idea of structure used in c. the class has the ability of supporting the concept of inheritance.
Class in c++ are similar to structures in c .the only difference being, class defaults to private access control, where as structure default s to public.
A class definition starts with the key –word class flowed 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.
Syntax
Class class _name
{
Private:
Data
and function;
Public:
Data
and function;
} ;
Example
Class room
{
Public:
Double length;
Double breath;
Double height;
Double calculate area ()
{
Return length *breath
}
Double
calculate volume ()
{
Return
length *breath
}
};
OBJECTS
Object is basic runtime entity in oop`s (object oriented programming) class is user defined type while object is a type of that variable so simply object can be define as ,it is combination of data and member functions which conform to the definition of a class. Object is the variable of type class. It can be created or destroy dynamically.
Syntax
Class name object _name list
In above syntax is name of class and object name is the list of valid identifiers and its must be separated by comma. The syntax of object is same as standard variable declaration.
Example
Student p1, p2;
Hera student is class name, while p1and p2 are two object structure of class can be immediately declared after declaration of class such as variable.
Class student
{
Private:
Int
rno, char name [10];
Public:
Void
get data ();
Void
put data ();
} p1, p2;
In this example rno, name, and fees are the data member and getdata (), putdata () are the member faction.
ACCESS MODIFIERS IN C++
Access modifiers are used to implement an important feature of object .oriented programming known as data hiding. The members of a class are declared in the member lost the member lost of a class may be divide into three number of private, protected and public sections using keywords known as access modifiers .c++ supports three type of mode may be called as visibility modifier or access specifier such as private, public protected .a colon [:] must follow the access specifier.
PUBLIC ACCESS SPECIFIER
Public means all the class members declared under public will be available to everyone .data member or member functions which are declared as public .we can directly access it by using the dot operator(.) with the object of the class.
Example
C++ program to
demonstrate public access modifier.
#include<iostream>
Using namespace std;
Class circle
{
Public
Double
radius ;
Double
compute area ()
{
Return 3.14*radius*radius;
}
};
Int main ()
{
Circle obj;
Obj.radius =5.5;
Cout <<”radius is :” <<
obj.radius <<endl;
Cout <<”area is :” <<
obj.compute area ( );
Return 0;
}
Output
Radius is: 5.5
Area is: 94.985
PRIVATE
ACCESS SPECIFIER
Private key word mean
that no one can access the class members declared private ,outside that class
.if someone try to access outside the it gives compile time error .by default
class variables and member functions are private. It only the member functions
or the friends functions are allowed to access the private data members of a
class. The main use of using private is to keep the data secure, and we
maintain it to the class internal representation only so that the data of the
user is secure.
Demonstrate private
access specifier.
#include<iostream>
Using
namespace std;
Class base
{
Private:
Int x;
Protected:
Int y;
Public:
Int z;
Base ()
{
x=10;
y=20;
z=50;
}
};
Class derive:
private base
{
Public:
Void showdata ()
{
Cout << “x is not accessible”
<< endl;
Cout << “value of y is ”
<< y << endl;
Cout <<”value of z is ”
<< z << endl;
}
};
Int main ()
{
Derive a; //object of derived class
a. Showdata ();
Return
0;
}
Output
X is not
accessible
Value of y is
20
Value of z is
50
PROTECTED ACCESS SPECIFER
It is similar
to private access specifier .it makes class member inaccessible outside the
class. But they can be accessed by any subclass of that class. Data members or
member functions which are declared as protected. Those members which are
declared in protected will be protected up to the next level after that those
becomes private.
Example
#include<iostream>
Using
namespace std;
Class example
1
{
Protected:
Int a;
};
Class example
2: public example 1
{
Public:
Void init_a (int a)
{
This->a=a;
}
Void print_a()
{
Cout << “a:” << a
<< endl;
}
};
Int main ()
{
example 2 ex;
ex.init_a(100);
ex.print_a();
return 0;
}
Output
a: 100
COPYRIGHT ©2020
#SAP PRODUCTION HOUSE
#COMPUTICSPLUS
No comments:
Post a Comment