oops concept
TRANSCRIPT
Sreya [email protected]
mwww.facebook.com/sreyatwitter.com/usernamein.linkedin.com/in/
profilename
OOPS CONCEPT IN C++
Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
Object Oriented Programming
Object-oriented programming (OOP) is a programming paradigm that uses “Objects“ and their interactions to design applications and computer programs.
Important in oops
• Object• Class• Data Abstraction • Encapsulation• Inheritance• Polymorphism
Object
• Object is the basic unit of object-oriented programming.
• Objects are identified by its unique name.
• An object represents a particular instance of a class.
Example : Car is an object
•An Object is a collection of data members and associated
member functions also known as methods.
object
Data membersModelColor
StartMoveStop
Methods
car
class• Class is basically blue print for object.
• It declare and define what data variables the object will have
and what operations can be performed on the class’s object.
Class name
Data variable
Functions
Structure of class
#include <iostream.h> class Square {
private: int side, a; public: int area( side) {
a =side*side; return a;
} void show()
{ cout << "The area is::" << a;
} }; void main() { Square x; //x is the object for class Square
x.area(10); x.show(); }
The area is:: 100
Data abstraction
• Abstraction refers to showing only the essential features of the
application and hiding the details.
• Example : calculator
• Any C++ program where you implement a class with public
and private members is an example of data abstraction.
#include <iostream>using namespace std;class Adder{ public: // constructor Adder1(int i = 0) { total = i; } // interface to outside world void addNum(int number) { total += number; } // interface to outside world int getTotal() { return total; }; private: // hidden data from outside world int total;};
int main( ){ Adder a; a.addNum(10); a.addNum(20); a.addNum(30); cout << "Total " << a.getTotal() <<endl; return 0;}
Out put
Total 60
Encapsulation• Encapsulation is also known as Data Hiding.• Encapsulation is the process of combining data and function
into a single unit called class.
class class name{private:datatype data;public:Member functions;};main(){classname objectname1,objectname2……………;}
Inheritance
• Inheritance is the process of forming a new class from an
existing class or base class.
• The base class is also known as parent class or super class.
• The new class that is formed is called derived class. Derived
class is also known as a child class or sub class.
Shape class hierarchy
TwoDimensionalShape
Shape
ThreeDimensionalShape
Circle Square Triangle Sphere Cube Tetrahedron
Implementation of inheritance
class derived-class: access-specifier base-class
•Syntax for implementing derived class
•Access Control and Inheritance
Access public protected private
Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no
Type of inheritance
• Single inheritanceIn this this type of inheritance one derived class
inherits from only one base class.
B
A
• Multiple inheritance
In this type of inheritance a single derived class may inherit from two or more base class.
BA
c
• Hierarchical Inheritance
in this method multiple derived classes inherits from a single base class.
B
A
C D
• Multi Level inheritance
In this type of inheritance the derived class inherits from a class, which in turn inherits from other class.
B
C
A
• Hybrid (virtual) inheritanceit is the combination of hierarchical and multilevel
inheritance.
B
A
C
D
Polymorphism
• The word polymorphism means having many forms.
• C++ polymorphism means that a call to a member function
will cause a different function to be executed depending on the
type of object that invokes the function.
Shape
Rectangle Ttiangle
area()
perimeter()
area()
perimeter()
area()
perimeter()
Triangle and Rectangle are child classes with their own separate implementations of the methods area() and Perimeter()
Shape is the super class with no implementation of area() and perimeter()
Example
#include <iostream>using namespace std;class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; }};class Rectangle: public Polygon { public: int area() { return width*height; }};
int main () { Rectangle rect; Triangle trgl; rect.set_values (4,5); trgl.set_values (4,5); cout << rect.area() << '\n'; cout << trgl.area() << '\n'; return 0;}
class Triangle: public Polygon { public: int area() { return width*height/2; }};
Out put2010
Thank you
Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many contents online?
Do you want to learn things quickly?
Tired of spending huge amount of money to become a Software professional?
Do an online course @ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed to make a quick, good software professional out of anybody who holds a passion for coding.
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Give a feedback @ massbaab.com/baabtra
Thanks in advance
www.baabtra.com | www.massbaab.com |www.baabte.com
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode
Start up VillageEranakulam,Kerala, India.Email: [email protected]
Contact Us