Oop concepts
-
Upload
baabtracom-no-1-supplier-of-quality-freshers -
Category
Technology
-
view
78 -
download
1
Transcript of Oop concepts
Abdul Raouf N
www.facebook.com/Abdul Raouf
twitter.com/username
in.linkedin.com/in/profilename
OOP CONCEPTS
Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
OOP CONCEPT
• Object-oriented programming (OOP) is a style of
programming that focuses on using objects to design
and build applications.
• Think of an object as a model of the concepts,
processes, or things in the real world that are
meaningful to your application
OBJECT
• Which will have a name as identity
• Properties to define its behaviour
• Actions what it can perform
• It has two main properties:
– State: the object encapsulates information about itself - attributes or fields.
– Behaviour: the object can do some things on behalf of other objects – methods.
OBJECT(contd)
• Example:
In a banking system, a particular bank account is an example of an object.
– Its state consists of attributes like: owner, account number, balance, etc.
– Its behaviours consist of: deposit, withdraw, etc.
CLASS
• We need to create a base design which defines the properties and functionalities that the object should have.
• In programming terms we call this base design as Class.
• We can create any number of objects from a class.
• Each individual object is called an instance of its class.
CLASS(contd)
• The actions that can be performed by objects become functions of the class and is referred to as Methods.
• No memory is allocated when a class is created. Memory is allocated only when an object is created.
• Example:
Banking system is an example for class.
Different accounts are example for objects.
How to create class in C++
class shape //create a class
{
public: Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
ATTRIBUTES
Contain current state of an object.
• Attributes can be classified as simple or complex.
• Simple attribute can be a primitive type such as integer, string, etc.
• Complex attribute can contain collections and/or references.
• Complex object: contains one or more complex attributes
METHODS
• Defines behavior of an object, as a set of encapsulated functions.
• The class describes those methods.
• It defines what an object can do.
INHERITANCE
Inheritance allows child classes inherits the characteristics of existing parent class.• Attributes (fields and properties)• Operations (methods)
Child class can extend the parent class.• Add new fields and methods• Redefine methods (modify existing behavior)
INHERITANCE-Example/* C++ Program to calculate the area of rectangles using concept of inheritance.
#include <iostream>
using namespace std;
class Rectangle{
protected:float length, breadth;
public:
Rectangle(): length(0.0), breadth(0.0){
cout<<"Enter length: "; cin>>length;
cout<<"Enter breadth: "; cin>>breadth;
}};
/* Area class is derived from base class Rectangle. */
class Area : public Rectangle{
public:
float calc(){
return length*breadth;
}};
int main(){
cout<<"Enter data for rectangle to find area.\n";
Area a;
cout<<"Area = "<<a.calc()<<" square meter\n\n";
return 0;
}
ABSTRACTION
• Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones.
• Abstraction = managing complexity.
• Allows us to represent a complex reality in terms of a simplified model.
• Abstraction highlights the properties of an entity that we need and hides the others.
ENCAPSULATION
• Encapsulation hides the implementation details
• Class announces some operations (methods) available for its clients – its public interface
• All data members (fields) of a class should be hidden-Accessed via properties (read-only and read-write)
Example for Abstraction and Encapsulation #include <iostream>
using namespace std;
class Adder{
public:// constructor
Adder(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;
}
POLYMORPHISM
• Polymorphism is the ability to take more than one form.
• Polymorphism allows abstract operations to be defined and used.
• Polymorphism allows routines to use variables of different types at different times.
Example for Polymorphism#include <iostream>
using namespace std;
class Shape { protected:int width, height;
public: Shape( int a=0, int b=0) { width = a; height = b; }
int area() { cout << "Parent class area :" <<endl; return 0; } } ;
class Rectangle: public Shape{
public:Rectangle( int a=0, int b=0):Shape(a, b) { }
int area (){
cout << "Rectangle class area :" <<endl; return (width * height);}};
class Triangle: public Shape{
public:Triangle( int a=0, int b=0):Shape(a, b) { }
int area (){
cout << "Triangle class area :" <<endl; return (width * height / 2);}};
// Main function for the program
int main( ){
Shape *shape; Rectangle rec(10,7); Triangle tri(10,5);
// store the address of Rectangle
shape = &rec;
// call rectangle area.
shape->area();
// store the address of Triangle
shape = &tri;
// call triangle area.
shape->area();
return 0; }
FUNCTION OVERLOADING
• It is simply defined as the ability of one function to perform different tasks.
• For example, doTask() and doTask(object O) are overloaded methods.
• To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field.
Example for Function Overloading#include <iostream>
// volume of a cube
int volume(int s){
return s*s*s;
}
// volume of a triangle
float volume(int b, int h){
return 0.5*b*h;
}
// volume of a cuboid
long volume(long l, int b, int h){
return l*b*h;
}
int main(){
std::cout << volume(10);
std::cout << volume(9, 7);
std::cout << volume(100, 75, 15);
}
In the above example, the volume of various components are calculated using the same function call "volume", with arguments differing in their data type or their number.
OPERATOR OVERLOADING
• Different operators have different implementations depending on their arguments.
• Operator overloading is generally defined by the language, the programmer, or both.
• We can redefine or overload most of the built-in operators available in C++.
Example for Operator Overloading
#include<iostream>
class complex
{
public: int real,imaginary;
complex operator+(complex ob)
{
complex t;
t.real=real+ob.real;
t.imaginary=imaginary+ob.imaginary;
return(t);
}
};
int main()
{
complex obj1,obj2,result;
obj1.real=12; obj2.imaginary=3;
obj2.real=8; obj2.imaginary=1;
result=obj1+obj2 // result=obj1.operator+(obj2);
cout<<result.real<<result.imaginary;
return 0;
}
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 designedto 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
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
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]