OOPs Notes Unit-2
OOPs Notes Unit-2
SUBJECT NAME: Object Oriented Programming using C++ SUBJECT CODE: BCA-301
NOTES
Unit -2
class A
{
public:
void dis();
void putstud()
{
cout<<"\n\t The student :"<<name<<" has Rollno "<<rollno ;
}
};
void main()
{
student x,y ; // 2 object of the student type(class)
clrscr();
cout<<"\n\t input for X ";
x.getstud();
cout<<"\n\t Input for Y";
y.getstud();
cout<<"\n\t\t To Display for X";
x.putstud();
cout<<"\n\t\t To Display for Y";
y.putstud();
getche();
}
2) Object
An object always represent an entity for the class. So we can declare an object using
the class type i.e. an user define data type
There are various characteristics of an object are as follows
a) An object is always declares of type class which is a user define type hence an object
declares using class type.
class A
{
};
void main()
{
A *p = new A() ; // p is an instance of class Type A
}
b) An object always invoke or calls the public member function (public members not the
private members) outside the class body mainly in the main().
c) To call or invoke the public member (public member function) the dot operator (.) use
and it is call or invoke by the pointer object then it may be call using arrow
operator( ) operator.
Example
Class A
{
Public:
void disA()
{
}
};
Main()
{
A x ; // simple object
A *p; // pointer object
x.disA(); // use dot opr
p disA(); // use arrow opr
}
d) An object when declares using “new” keyword then an object is called an “instance”.
e) We can also declare an array of object using class type in the same way declaring
primitive type array
Class A
{
}; // end of the class
Void main()
{
A x[10] ; // an array of an object
}
f) Where there is no explicit constructor defines by the user then object declaration calls
an implicit default constructor.
g) There can be multiple object defines of the single class and each object has its own
data member value which need to be initialise.
Example
#include
<iostream.h>
#include <conio.h>
class student
{
int roll;
char name[20];
public:
//default constructor
student()
{
roll=11101;
strcpy(name,"Raman kumar");
}
//parameterized constructor
student(int r, char *n)
{
this->roll=r ; //roll=r ;
strcpy(name,n);
}
void display()
{
cout<<"\n\t The "<<name<<" has rollno:"<<roll;
}
};
void main()
{
student x ; // calls default
student y(3124,"Gagan sharma");
clrscr();
x.display(); // print x data member
values y.display(); // print y data member
values getche();
}
Define an attributes in c++ class
In C++ , class has 2 parts or section. In the first section the private data member or an
instance variable declares in the form of an attributes or fields and in the second section the
public member function or the methods defines.
In C++ class the declares attribute plays very important part since an attributes declares in the
form data members or an instance variable
1) An attribute (data member) can be declares of any C/C++ Data types. It can be either
any primitive type(int,float,char,double) or user define type(struct,union,class)
2) An attribute can be declares in the form of an array also of any type.
3) By default all these attribute or data member can be declares as private, So they can’t
be access outside the class body or in the main() using the class object or an instance
implementing the oops concept data hiding or information hiding
4) Since these private data members(an attributes) can’t be access outside the class body
via an object of the class type then all attributes can only be use or access inside the
class body via public member function implementing an oops concept known as
encapsulation
class student
{
// private data members(attributes)
int rollno;
char name[20];
public:
student(int r, char *n)
{
rollno=r;
strcpy(name,n);
}
}; // end of the class
In this example 2 attributes declares of primitive type which is private so can’t
be visible outside the class body. Then it can only be used by the public member
function i.e. used inside define constructor implementing the oops concept
encapsulation.
5) By default all the declare attribute/data members are non static hence the value
of different objects are different since all the non-static attributes are declares for
an object.
#include <iostream.h>
#include <conio.h>
class A
{
int a,b,s ;
public:
A()
{
a=55;
b=33;
}
A(int a,int b)
{
this->a=a;
this->b=b;
}
void disp()
{
cout<<"\n\tA is:"<<a;
cout<<"\n\tB:"<<b;
}
};
void main()
{
A x ; // calls default constructor
A y(76,45);
clrscr();
cout<<"\n\tX is :";
x.disp();
cout<<"\n\tY is :";
y.disp();
getche();
}
In this example the values of attribues a and b for object X is 55 and 33 and for object
Y are 76 and 45
6) An attribute are also called data member when simple object declares but when
object declares using “new” keyword then it an object is called an instance and an
attributes becomes an instance variable.
7) The value of an attributes can also be initialise using constructor. (see an example of
point 5 where default and parameterized constructor use)
8) We have static data member (attribute) also which only declare for the class only not
for an object. It can be declare inside the class body but can be initialise outside the
class body using scope resolution operator (::). The value of these static attribute can
be share by every object of the class.
/*
An example of the static data member
*/
#include
<iostream.h>
#include <conio.h>
class A
{
static int count; // declaring static data member
public:
A() // default constructor
{
count++;
cout<<"\n\t"<<count<<" object creates ";
}
A(char *n) // parameterised constructor
{
count++;
cout<<"\n\t"<<count<<" object creates ";
}
~A() // destructor
{
count--;
cout<<"\n\t"<<count<<" object left ";
}
}; //end of the class
int A::count=0; // static data member initialise to 0
void main()
{
clrscr();
A x ; // calls default
A y("Raj");
//destructor calls here
}
Example
class emp
{
char ename[20];
public:
emp(int r, char *n) // constructor
{
}
~emp() // destructor
{
}
}; // end of the class
7) The define member function in the class body can be either
a) Non static: The non-static member function can be define only for an
object. Means the same non-static member function can be call multiple
times by different object having different this value.
b) Static member function: The static member function can be define for the
class only. Hence it can also be call via class_name
/*
An example of the static and non-static
membver function
*/
#include <iostream.h>
#include<conio.h>
class A
{
public:
//defining static method
static void staticmth()
{
y.nonstat();
A::nonstat(); // error cant call non static method by class
A::staticmth();
getche();
}
Define C++ class Declaration with example
To implements various oops concept we have to define the class because all oops concept
implements by using an object or by an instance. And since object declares by class type then
to declare an object we have to first define the class.
State identity and behaviour of an object
An object is the main concept of the oops.it is declare using the class type means an object
always declare using class type.
1) An object represent in the form an entity.
2) An object is the main oops concept in c++ and an object always represent the class
3) All the public define member function of the class can be call or invoke via an object
in the main() or anywhere outside the class.
4) An object declares using the class type i.e. the user define type.
5) Inside the member function “this” pointer always stores an address of an
invoking object
6) We can declares as many object of the same class type in the main()
class A
{
}
void main()
{
A x,y,z; // declares 3 object
}
Solution
In C and C++, we can pass an argument in user define function. An actual
argument can be pass in function call or when any member function call using an
object and formal parameter can be pass when function define. The data type of
both parameter and argument must be same because the value of actual argument
stores in formal parameter.
But default value can also be pass in the formal parameter which can only be use
then, when no value from actual argument pass to formal parameter.
So when the formal parameter initialises with some value then it is called the default
parameter value.
And this default parameter value can only be used in function definition when no
value pass from function call in the form of an actual argument.
An example of the default parameter
Solution: Usually in c++ an object declares using class user define type. So for that
we have the class first and then declares an object.
Normally when a simple variable declares using the class type then it is called an
object declaration.
But when any object of the class type declares using “new” keyword then it is called
an instance declaration and the process is called an instantiation
In, this memory is allocated for that object and the class constructor run.
When we declare the simple object of the class type then it is created on the stack
But when an instance create then its memory creates at heap. The memory creates
using constructor for an object. When an instance calls then constructor calls.
In the instantiation process, all the data member are called an instance variable
An example
/*
An example of an instantiation in oops
*/
#include<iostream.h>
#include<conio.h>
#include <string.h>
class student
{
int roll;
char name[20];
public:
student() // default constructor
{
roll=2134;
strcpy(name,"gagan singh");
}
student(int r, char *n) // parameterized constructor
{
roll=r;
strcpy(name,n);
}
student(student &s)//copy constructor
{
roll=s.roll;
strcpy(name,s.name);
}
void display()
{
cout<<"\n\t The name "<<name<<" has "<<roll;
}
~student() // destructor
{
cout<<"\n\tBye "<<roll;
}
}; //end of the class
void main()
{
student *p= new student() ; // usinjg default constructor
student *q= new student(6754,"Ramesh Verma"); //using the parameterised
constructor
student *r= new student(*q) ; //using the copy constructor
clrscr();
p->display();
q->display();
r->display();
delete p; // calls destructor for p
delete q; // q
delete r; // r
getche();
----------------------------------------------------------------------------------------------
}
};//class end
6) We can have multiple constructor in the same or in single class of the same name
since constructor name is same class name.
7) When defining multiple constructor which having the same name but having different
number of argument and their type then this is called constructor overloading
8) Constructor neither be define as virtual nor it defines as a friend function
9) Constructor can’t be static since rather they defines non-static since they always
defines for an object not for the class
10)Types of constructor
There are basically 3 types of constructor in c++ oops
(i) Default constructor: when no parameter pass in the constructor then it is
called the default constructor. Hence no value is passed in the form of actual
argument when declaring an object. So the data members initialises from
default values for any given object.
Example
class student
{
int roll;
char name[20];
public:
student() // default constructor
{
roll=3213;
strcpy(name,”Ram Sharma”);
}
} ; //end of clas
main()
{
student x; // calls default
student *p= new student(); // also call default constructor
}
(ii) Parameterised constructor: when in constructor the parameter pass in same
number and of same type as declares data member of the class then the define
constructor is called parameterised constructor. Then we have to pass the
value when declaring an object /instance for the data member.
Means data member value initialises from the pass value in the parameterised
constructor
Example
class student
{
int roll;
char name[20];
public:
student(int r, char *n) // paramaterised constructor
{
roll=r;
strcpy(name,n);
}
} ; //end of clas
main()
{
student x(5467,”sanjay sharma”); // calls parameterised
student *p= new student(6743,”pankaj”); // also call parameterised
constructor
}
(iii) Copy constructor: when object declares and pass another already
declares object in the form of an actual argument then this object
declaration automatically invoke copy constructor if define in the class
body by the user.
So the copy constructor take the reference of the class type object in the form
of the parameter.
The copy constructor does member by member copy of pass argument
reference object to this(invoking) object data member value
Example
class student
{
int roll;
char name[20];
public:
student(int r, char *n) // paramaterised constructor
{
roll=r;
strcpy(name,n);
}
student(student &s) // copy constructor
{
roll=s.roll;
strcpy(name,s.name);
}
} ; //end of clas
main()
{
student x(5467,”sanjay sharma”); // calls parameterised
student y(x) // calls copy constructor copies x data member to y data member
}
class student
{
int roll;
char name[20];
public:
student() // default constructor
{
roll=2134;
strcpy(name,"gagan singh");
cout<<"\n\t This is default constructor";
}
student(int r, char *n) // parameterized constructor
{
roll=r;
strcpy(name,n);
cout<<"\n\t This is parameterised constructor";
}
student(student &s)//copy constructor
{
roll=s.roll;
strcpy(name,s.name);
cout<<"\n\t This is copy constructor";
}
void display()
{
cout<<"\n\t The name "<<name<<" has "<<roll;
}
~student() // destructor
{
cout<<"\n\tThis is destructor";
cout<<"\n\tBye "<<roll;
}
}; //end of the class
void main()
{
clrscr();
student x ; // using default constructor
student y(6754,"Ramesh Verma"); //using the parameterised constructor
student z(y); //using the copy constructor
x.display();
y.display();
z.display();
//At this point the destructor calls for x ,y ,and z object
getche();
}
Define Destructor with example
The destructor is also the special member function like the constructor but its execution is
different from the constructor. Constructor creates but destructor destroy.
Destructor is the special member function which frees or destroys the allocated memory.
1) To define the destructor same as constructor we use the “class name”. So like
constructor the name of the destructor is also same class name.
2) There can be only one destructor define in the single class for all the objects. So we
don’t have any destructor overloading concept.
3) Constructor calls when object declares but destructor automatically calls or invoke
at the end of the program or the main(). it doesn’t require any explicit invocation.
4) The destructor calling sequence is just opposite to constructor calling. Means the
destructor of the last object declaration calls first and first object declaration
destructor calls first.
5) To define the destructor member function there is a use tilde(~) sign put before the
destruction name.
Example
class A
{
public:
~A() // define the destructor
{
}
};
6) Like constructor, destructor also can’t return any value so there is not any return type
in destructor also.
7) No parameter can be pass in destructor unlike constructor.
Calls at the time of object declaration It calls at the end of the main()
automatically. No need for an object
declaration
First object declaration constructor calls Last object declaration destructor calls
first and then so on first and so on
Can have different types of constructor No types
such as default, parameterised, copy
----------------------------------------------------------------------------------------------------------------
Explain an abstract class/Meta class in c++
Or
Explain pure virtual function in c++
Solution
Usually if the class defines by the user then its object creates but abstract class are those
class whose object/instance can’t be created. So an abstract class are those class which can’t
be instantiated.
And in c++ to create an abstract class we need to use the concept of pure virtual function
(abstract member function).
And an abstract class can always be the base class only since this concept always be
implement using inheritance oops concept.
(a) The pure virtual function only declares in the base class it can be define or over ride
in its derived class.
(b)The class where the pure virtual function only declares by default or automatically
becomes an abstract class which must also be the base class and then this abstract
class object or an instance cannot be created or declare in the main()
(c) To declare the pure virtual function there is the use of keyword “virtual” before
return type and also use “=0;” at the end of the declaration.
(d)Syntax
virtual <return_type> <function_name>(parameter)=0;
example
void sum(int,int)=0;
(e) In the abstract class we can also define the concrete method
(f) If no instance declare of the abstract class then its concrete member function can
now be invoke or call using its derived class object.
(g)We can override the pure virtual function in as many as classes depending of the
type inheritance using.
(h)
Full example after commenting the above error code
/*
An example of the pure virtual function or an abstract class
*/
#include
<iostream.h>
#include <conio.h>
class abs
{
protected:
int a,b,s ;
public:
abs(int m, int n)
{
a=m ;
b=n;
}
void display() // defining the concrete method
{
cout<<"\n\t A:"<<a;
cout<<"\n\t B:"<<b;
}
//declaring the pure virtual fnc
virtual int sum()=0;
virtual int minus()=0;
}; // end of the abstract class
class inh:public abs
{
public:
inh(int a,int b):abs(a,b) // calls base class constructor
{
}
int sum()
{
s=a+b ;
return s ;
}
int minus()
{
s=a-b ;
return s;
}
Solution: In C++ , object can be declare using the class type , means we have to create
the class first and then of that class type (user define data type) an object declares. So
the main concept is that an object type is the class type.
An object type also can be define in term of encapsulating the private data member
through member function.
Since the data member declares using as private and these private data member
(attributes) can be access by public member function these concept is called oops
concept encapsulation.
So an object type is a user define composite data type that encapsulate the data
member with the function which manipulates the value of the data member for any
object or an instances.
#include
<iostream.h>
#include <conio.h>
#include <string.h>
class emp
{
int ecode;
char name[20];
float sal;
public:
emp() // default constructor
{
ecode=2141;
strcpy(name,"Ram Sharma");
sal=67000;
}
emp(int e, char *n, float s) //parameterised constructor
{
ecode=e ;
strcpy(name,n);
sal=s ;
}
void displa()
{
cout<<"\n\tThe employee:"<<name<<" having code:"<<ecode<<" draws:"<<sal<<"
salary";
}
};//end of the class
void main()
{
emp x ; // calls default constructor
emp y(5678,"Jai Veeru", 61000);// calls parameterized constructor
clrscr();
proc
y.displa();
getche();
}
In this example the private data member encapsulates by member function constructor
and displa() for the given object x and y of class employee. This process is called an
object type.
Q find out an area of rectangle, triangle and circle using function overloading?
Solution: (year 2022)
/* To find out an area of circle , rectangle and triangle using fucntion
overloading
*/
#include <iostream.h>
#include <conio.h>
int area(int,int);
float area(float);
float area(float,float);
void main()
{
int l,b,rectar;
float r,circlear;
float bs,hg,triar;
clrscr();
l=20;
b=14;
rectar=area(l,b);
float area(float r)
{
float ar ;
const float pi=3.14;
ar=pi*r*r;
return ar;
}
float area(float b,float h)
{
float ar ;
ar=(b*h)/2;
return ar;
}
output
/*
Write the C++ program to print the folloing series
0+1+2+3+4+5+6+7+8+9+…..+N (year-2022)
*/