0% found this document useful (0 votes)
16 views43 pages

Unit 3

1. An object is a real world entity that can be uniquely identified and has properties, state, and behavior. There are different types of objects like global, local, static, and dynamic objects. 2. A class defines the blueprint for an object with data members, member functions, and access specifiers like public, private, and protected. The main components of a class are its name, attributes, and methods. 3. Inheritance allows classes to establish a hierarchical relationship to share and inherit common properties. There are different types of inheritance like single, multiple, multilevel, hierarchical, and hybrid inheritance.

Uploaded by

Nipun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views43 pages

Unit 3

1. An object is a real world entity that can be uniquely identified and has properties, state, and behavior. There are different types of objects like global, local, static, and dynamic objects. 2. A class defines the blueprint for an object with data members, member functions, and access specifiers like public, private, and protected. The main components of a class are its name, attributes, and methods. 3. Inheritance allows classes to establish a hierarchical relationship to share and inherit common properties. There are different types of inheritance like single, multiple, multilevel, hierarchical, and hybrid inheritance.

Uploaded by

Nipun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Object

Object is a real world entity that can be uniquely identify. Each


object have an identity, state behavior & property.

Types of object:
• Global (external) object.
• Local (automatic) object
• Static object
• Dynamic object
Object

Ex: class Student class


{
int rollno; data member
Char name[10]
Void getdata( ) Member function
{
cout<<“ Enter rollno and Name”;
cin>>rollno>>name;
}
Void show data()
{
Cout<<“roll”<<rollno<<“name”<<name;
}
};
Void main()
{
clrscr();
Student s1 object
S1.getdata();
S1.showdata();
getch();
Class
• A class is define by three element:
1. A unique class name
2. Data member ( attributes)
3. Member function( method)

• Class have 3 part:


1. Public
2. Private
3. Protected

Note:
a) Public part is accessible out side the class
b) Private and protected part is not accessible outside the class
• Syntax:
Class class_name
{
Private:
Data member;
Member function;

Protected:
Data member;
Member function;

Public:
Data member;
Member function;

};
class

Ex: class Student class


{
int rollno; data member
Char name[10]
Void getdata( ) Member function
{
cout<<“ Enter rollno and Name”;
cin>>rollno>>name;
}
Void show data()
{
Cout<<“roll”<<rollno<<“name”<<name;
}
};
Void main()
{
clrscr();
Student s1 object
S1.getdata();
S1.showdata();
getch();
Varieties of classes
• Abstract class
• Container class
• Generic class
• Base class
• Drive class
• Super class
Scope resolution operator:
#include<iostream.h>
#include<conio.h>
class A
{
public:
void fun(); // Only declaration
};
void A::fun() // Definition outside class using ::
{
cout << "function called";
}
int main()
{
A a;
a.fun();
Encapsulation
class Student
class
{
Student int rollno;
Date Members
Roll no Char name[10];
Name Int age;
Age Void getdata( int a, int b, int c)

{
rollno=a;
name=b;
Members Function age=c;
getdata() }
setdata() Void show data()
{
Cout<<“roll”<<rollno<<“name”<<na
me>>”age”<<age;
} encapsulation
Pictorial representation and , Ex of
Inheritance
Polymorphism
Class A
{
Int a;
Void show() //show in base class
{
- ------
-- - - - - -
}
};
Class B: public A
{
Void show() //show in derive class
{
- - - - - -- -
- - - - -- -
}
};
Operator overloading
Function overloading
Function overloading means two or more functions
can have the same name but either the number of
arguments or the data type of arguments has to be
different. Return type has no role because function
will return a value when it is called and at compile
time compiler will not be able to determine which
function to call. in this program we make two
functions with identical names but pass them
different number of arguments. Function
overloading is also known as compile time
polymorphism.
C++ program for function overloading:
#include <iostream.h>
#include<conio.h>
Class Test
{
int r,h,w;
void area (int x)
{
r=x;
cout<<“Area of circle is”<<3.14*r*r;
}
Void area(int a, int b)
{
h=a;
w=b;
cout<<“Area of rectangle is”<<h*w;
}
};
void main ()
clrscr();
Test t1;
t1.area(5);
t1.area(5,10);
getch();
}
Operator overloading
Run Time Polymorphism
Run Time Polymorphism Achieve through Virtual Function.
Virtual Function program in c++:

#include <iostream.h>
Class Base
{
Public:
Virtual void Display()
{
Cout<<“Display Base”;
}
void Show()
{
Cout<<“Show Base”;
}
};
Class Drive: public Base
{
Public:
Void Display()
{
cout<<“Display Drive”;
}
Void Show()
{
Cout<<“Show Drive”;
}
Run Time Polymorphism
Void main()
{
Base b1, *bp;
Derive d1;
bp=&b1;
bp Display();
bp Show();
bp=&d1;
bp Display();
bp Show();
getch();
}
Inheritance
Inheritance
Types of Inheritance
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hybrid Inheritance
5. Hierarchal Inheritance
Single Inheritance

Class base class name


{
Data member;
Member function();
};
Class drive class name: visibility mode base class name
{
Data member;
Member function();
};
multiple Inheritance

Class base class name1


{
Data member;
Member function();
};
Class base class name2
{
Data member;
Member function();
};

Class drive class name: visibility mode base class name1, visibility mode base class name1
{
Data member;
Member function();
};
Multilevel Inheritance

Class base class name


{
Data member;
Member function();
};
Class drive class name1: visibility mode base class name
{
Data member;
Member function();
};

Class drive class name2: visibility mode drive class name1


{
Data member;
Member function();
};
Hierarchical Inheritance

Class base class name


{
Data member;
Member function();
};
Class drive class name1: visibility mode base class name
{
Data member;
Member function();
};

Class drive class name2: visibility mode base class name


{
Data member;
Member function();
};
Class drive class name3: visibility mode base class name
{
Data member;
Member function();
Hybrid Inheritance

Class base class name


{
Data member;
Member function();
};
Class drive class name1: visibility mode base class name
{
Data member;
Member function();
};

Class drive class name2: visibility mode base class name


{
Data member;
Member function();
};
Class drive class name3: visibility mode drive class name1, visibility mode drive class name2,
{
Data member;
Member function();
Single inheritance
#include <iostream.h>
#include<conio.h>
Class A
{
int a,b
void getdata ()
{
cout<<“enter two value”;
Cin>>a>>b;
}
};
Class B:Public A
{
Int c;
Void showdata()
{
c=a+b;
Cout<< “addition is”<<c;
}
};
void main ()
clrscr();
B b1;
b1.getdata();
b1.showdata();
getch();
}
CONSTRUCTOR
• A constructor is a member function of a class which initializes
objects of a class. In C++,Constructor is automatically called when
object(instance of class) create. It is special member function of
the class.

Types of Constructors:

• Default Constructor.
• Parameterized Constructor.
• Copy Constructor.
Default Constructor.
#include <iostream.h>
#include<conio.h>
class text
{
public:
text()
{
Cout<<“Default constructor is called”;
}
};
Void main()
{
clrscr();
text t1;
getch();
}
Parameterized constructor
#include <iostream.h>
#include<conio.h>
class Area
{
private:
Int height,width;
public:
Area(int x,int y) //parameterized constructor
{
height=x;
width=y;
}
Area()
{
return(height*width);
}
};
Void main()
{
Area a1(10,20);
cout<<“area is ”<<a1.area();
getch();
}
#include <iostream.h> Copy constructor
#include<conio.h>
Class Test
{
Private:
Int a;
Public:
Test();
Test(int x)
{
a=x;
}
Test (test &obj)
{
a=obj.a;
}
Void showdata()
{
Cout<<a;
}
};
Void main()
{
Int x;
Cout<<“enter some number”;
Cin>>x;
Test t1(x);
t1. showdata();
Test t2(t1);
t2.showdata();
getch();
}
DESTRUCTOR
#include <iostream.h>
#include<conio.h>
Class Test
{
Public:
int a, b, c;
Test(int x, int y)
{
a=x;
b=y;
}
Void showdata()
{
c=a+b;
Cout<<“addition is ”<<c;
}
~Test()
{
Cout<<“Destructing the object”;
}
};
Int main()
{
Test t1(10,20);
t1.showdata();
}
FRIEND FUNCTION
A friend function of a class is defined outside that class' scope but it
has the right to access all private and protected members of the
class. the compiler known a given function is a friend function by its
keyword friend. the deceleration of friend function should be made
inside the body of class starting with keyword friend.
Syntax:
Class Class_name
{
Friend return_type Function_name(arguments);
};
Friend return_type Function_name(arguments);
{
Body of function
……………..
………………
#include<iostream.h>

#include<conio.h>
class base
{
int a, b;
public:
void get()
{
cout << "Enter two values:";
cin >> a>>b;
}
friend float avg(base ob);
};
float avg(base ob)
{
return float(ob.a + ob.b) / 2;
}
void main()
{
clrscr();
base obj;
obj.get();
cout << "\n Mean value is : " << avg(obj);
getch();
}
Data structure

Data Structure is a way of collecting and organizing


data in such a way that we can perform
operations on these data in an effective way.
Or
Data structure is an arrangement of data in
computer memory or even disk storage.
Or
Data structure is a named group of different data
types which can be processed as a single unit.
Types of Data Structure
Data structure operation
1. Traversing
2. Searching
3. Inserting
4. Deleting
5. Sorting
6. Merging
7. Copying
8. Concatenation
Array
Stack
Queue
Link list
Non linear data structure
Graph

You might also like