CPP Chap 2complete
CPP Chap 2complete
2.1 Defining Class, Members, Object <write any example using class & object>
Page #1
<write any example using class & object> {
cout<<”Addition=”<<a+b<<endl;
cout<<”Subtraction=”<<a-b<<endl;
cout<<”Division=”<<a/b<<endl;
cout<<Multiplication=”<<a*b;
2.6 Constructor & Destructor }
- As C++ Language belongs OOP category hence };
uses class and object as it’s most basic void main( )
programming entities. {
- A program code created using such class and ConstDemo ob; // creating object and calling constructor
object, needs to perform some background ob.output( );
memory related operations. }
- For this every class created uses it’s two special - Types of constructor
functions: 1. Default constructor
o Constructor 2. Parameterized constructor
o Destructor 3. Copy constructor
Constructor – it is one special function associated
with every class created in OOP code. 1. Default constructor – when a constructor is
- This special function perform following two defined without parameter, it is called default
special operations during execution constructor.
o Allocation of memory a. Syntax:
o Initialization of memory class <ClassName>
- Constructor is one implicitly created and called {
Function. public:
- But in case, if constructor to be defined, <ClassName> ()
following rules must be considered. {
It does not uses return data type ---- // Default constructor}
Name of constructor function and class };
name must be same e.g.
It may with or without parameter. class DefConstDemo
Constructor never returns value {
It must be under public access. int a;
It is implicitly called when class public:
instantiated i.e. object on class is created. DefConstDemo( )
o Syntax: {
class <ClassName> a=100;
{ }
public: void output( )
<ClassName> ( [ parm ] ) {
{ cout<<”Value of a assigned by constructor=”<<a<<endl;
---- // constructor definition }
} };
}; void main( )
- a constructor function always performs it’s {
predefined task i.e. allocation and initialization ConstDemo ob; // creating object and calling constructor
and also can perform other task defined in it like ob.output( );
taking input, giving output, etc. }
- e.g. 2) Parameterized constructor – this is the
class ConstDemo constructor defined with parameter.
{ - A constructor can use single parameter or
int a,b; multiple parameters.
public: - When such, parameterized constructor is
ConstDemo( ) defined, the required values must be passed at
{ the time of creating object.
cout<<”Input two integers=”; - Also, the multiple parameters can be of same
cin>>a>>b; type or different types.
} e.g.
void output( ) class ParamConstDemo
Page #2
{ class ClassName
int a; float m; {
public: public:
ParamConstDemo(int x, float y ) ~ClassName( )
{ {}
a=x; m=y; // initializing data member };
} - E.g.
void output( ) class Demo
{ {
cout<<”Value of a assigned by constructor=”<<a<<endl; int a;
cout<<”Value of b assigned by constructor=”<<m<<endl; public:
Demo( )
} {
}; a=100;
void main( ) }
{ ~Demo( )
ConstDemo ob (100, 3.55); // creating object and calling {
constructor by passing values. cout<<”\n\n Destructor Executed”;
ob.output( ); }
} void output( )
{
3. Copy constructor – this type of constructor is cout<<”Value of a assigned by constructor=”<<a<<endl;
used to copy the contents of one object to other. }
E.g };
A ob1, ob2; //creating two objects on class A void main( )
ob2=ob1; // in this line contents of object 1 copied to {
object 2. Demo ob; // creating object and calling constructor
ob.output( );
- C++ also supports constructor overloading. By }
this, in same class multiple constructors can be
defined by same name but different signature. 2.6 Visibility Modes / Access Modifiers / Access
Destructor – it is one special function associated Specifiers / Visibility Labels
with every class created in OOP code. - As CPP programming language belongs to OOP
- This special function when called it performs technique, it ensures data security.
two special tasks of de-initialization & de- - As OOP language, CPP language supports visibility
allocation of memory. modes.
- When object is created, for this memory is - Visibility modes are special keywords that decides how
allocated and this memory is in use till the and which member of class to be accessed from where?
program is in execution. - CPP supports following visibility labels:
- Once the program execution is completed, this 1. private 2. protected
allocated memory must be reclaimed, else it will 3. friend 4. Public
cause wastage of memory. 1. private – this keyword of OOP as visibility mode
- For this, CPP code uses destructor to free the specifies that, the member under this are visible or
unused memory. accessible only with-in class & not outside the class.
- Destructor function shows following special - Generally the data member for data security are kept
features: under private mode.
o Destructor name must be same as class - This is the default visibility mode used by C++
name but preceded by tilde character compiler when no other visibility keyword is used.
(~). e.g.
o Destructor never returns any value class A
hence it doesn’t uses return type. {
o Destructor never uses any parameter. private:
o Destructor must be defined under public int x;
section. };
o Destructor get called implicitly
whenever program goes out of scope.
Syntax:
Page #3
2. protected – any member of class if under protected - to create such static members, C++ language supports
visibility mode, such member is accessible outside the a special keyword i.e. static.
class but only from derived or child class.
- This mode is commonly used while applying - Members created in class using “static” keyword
inheritance in OOP code. called as static members.
class A - A C++ class can use following static members:
{
protected: Static Data Member
int x; Static Function Member
};
Static Data Member
3. friend – this visibility mode is used to declare a - By default the data member of class is non-
function as friend of class so that, such friend function static and due to this, when multiple objects of
can access the member of class from outside.
class created, multiple copies of such data
Syntax:
friend ReturnType FunName ( [ param ] ); member is created for each object.
class A - But, in practical reality, only one object will be
{ in execution and other object will be in idle
private:
int x; state.
friend void display( ); - Like:
}; class A
{
4. public – any member of class which under public
visibility mode is accessible with-in as well as outside int n;
the class. -----
- Generally the member functions which needs to be -------
called from outside, are under public mode.
-----
class A } ob1, ob2, ob3;
{ - Thus in above e.g., on class A, 3 objects are
private:
created and hence each object will gets
int x;
public: individual copy of data member as:
void display( )
{
----
}
};
-
- Due to this, it causes too much of memory
wastage.
- To avoid this memory wastage data member to
be declared as static.
- When a data member in class declared using
“static” keyword, it becomes static data
member.
- To create and use such static data
member , following are the rules:
1. Static data member must be declared with class
2.3 Static members
as:
- A class created in CPP program may encloses data and
Syntax: static DataType VariableName;
function members.
e.g. static int n;
- by default such members are non-static but in some
cases these members to be used in static form.
Page #4
2. Such static member must be defined outside int Demo::n; // defining static data member
the class using scope resolution operator (::) as: void main( )
{
Syntax : DataType ClassName::VariableName; Demo::display();//calling static member
e.g. int A::n; }
- If the data member is static, it shows following
features: 2.4 Pointer to members & Pointer to objects
1. it gets initialized to zero implicitly. - As C++ belongs to OOP category hence it allows to
2. only one copy will be created for multiple create and use class and objects.
objects as: - Normally, when a class is created with members, these
members can be accessed outside the class.
- As per general practice, function members of class are
kept as public and hence can be accessed from outside
using an object.
- When an object is declared, it is used to access class
member using Member Accessing operator (.) as:
3. static data member can be accessed only by ObjectName.Member ( )
static function member. - As class is datatype hence it also allows to create
Thus, as the member is static, only one copy for all pointer object as:
objects will be created, hence there will not be ClassName *ObjectName;
memory wastage but more memory usage. Demo *ob;
- When pointer object is created, it can access member
Static function Member
of class using a special operator i.e. “this operator (->)”
- By default, function member of class is non-static. But
as:
in some cases, a function to be created staticfunction
ObjName -> MemberName;
as:
ob->display( );
static RetType FuncName( [param])
e.g.
{
class Demo
---- {
} int a;
- If the function member is static, it shows following public:
features: void input( )
{
1. it can be called from outside without using object, cout<<”Enter a number=”;
but needs class name and scope resolution operator(::) cin>>a;
as: }
void output( )
Classname::FunctionName( ); {
A::display(); cout<<”Square of ”<<a<<”=”<<a*a<<endl;
2. It can access or operate only on static members. cout<<”Cube of ”<<a<<”=”<<a*a*a<<endl;
e.g. }
};
class Demo void main( )
{ {
static int n; Demo *ob; // creating pointer object and calling constructor
public: ob.input( );
static void display( ) ob.output( );
{
n=5; }
cout<<”Square=”<<n*n;
}
};
Page #5
2.7 Friend Function
- As CPP programming language belongs to OOP End of Unit-2
technique, it ensures data security.
- As per standard practice, the data member of class
private & hence it is accessible only with-in class by it’s
members and not outside the class.
- But in some cases, such members allowed to be
accessed outside the class using a special visibility mode.
- in such case OOP allows to create and use friend
function.
- When a function is declared using friend keyword with-
in class it becomes friend function and such function can
access private members of class.
Rule to create and use Friend Function:
1. It must be declared with-in class using friend
visibility label as:
friend RetType FunName( [ parm } );
2. it must be defined outside the class.
3. It can access private member of class using it’s
object.
4. Such function can be called from main method
without any object of class.
e.g.
class Demo
{
int a;
public:
void square( ) // member function
{
cout<<”Enter a number=”;
cin>>a;
cout<<”Square of ”<<a<<”=”<<a*a<<endl;
}
friend void sqroot( ); // declaring friend function
};
void sqroot( )
{
Demo ob1;
cout<<”Enter a number=”;
cin>>ob1.a;
Page #6