The document discusses object-oriented programming concepts including classes, objects, encapsulation, data hiding, inheritance, polymorphism, and overriding. Specifically:
1. A class defines the data (attributes) and functions (behaviors) that characterize concepts in the problem domain. An object is an instance of a class that allocates memory.
2. Encapsulation groups related data and functions into a class. Data hiding uses access modifiers like public and private to restrict access to some class components.
3. Inheritance allows new classes to reuse and build upon existing classes through mechanisms like base and derived classes. Polymorphism allows different outputs from functions with the same name but different parameters through method overloading and
2. OOPS
Class – group of data members & member functions
Like person can be class having data members height and
weight and member functions as get_details() and
put_details() to manipulate on detailsput_details() to manipulate on details
Class is nothing until you create it’s object
Object – instantiates class allocates memory
C
SharpC
ode.org
3. OOPS Fundamentals…
Access to data members & member functions can be done
using object only (if they are not static!)
OOPS features are
1. Encapsulation1. Encapsulation
2. Data hiding
3. Data reusability
4. Overloading (polymorphism)
5. Overriding
C
SharpC
ode.org
4. OOPS Features…
Encapsulation – making one group of data members &
member functions
Can be done through class
Then group of data members & Member functions will beThen group of data members & Member functions will be
available just by creating object.
C
SharpC
ode.org
5. OOPS Features…
Data Hiding – can be done through access modifiers
Access modifiers are private, public, protected and internal
Private members or member function won’t be available
outside classoutside class
Public – available all over in program outside class also
C
SharpC
ode.org
6. OOPS Features…
Protected – members that are available in class as well as in
it’s child class
Private for another class
Protected access modifier comes only when inheritance isProtected access modifier comes only when inheritance is
in picture
Internal is used with assembly creation
C
SharpC
ode.org
8. OOPS Features…
Overloading – taking different output of one method or
operator based on parameters and return types
Like add() method performs addition and add(int a,int b)
performs addition of ‘a’ and ‘b’ passed when callingperforms addition of ‘a’ and ‘b’ passed when calling
Also, + operator performs addition of two numbers as well
as concatenation of strings
C
SharpC
ode.org
9. class arith
{
public:
void calc(int num1)
{
cout<<"Square of a given number: " <<num1*num1 <<endl;
}
void calc(int num1, int num2 )
{
cout<<"Product of two whole numbers: " <<num1*num2 <<endl;
}
};};
int main() //begin of main function
{
arith a;
a.calc(5);
a.calc(6,7);
}
This is example of method overloading, output will be
Square of given number : 25
Product of two whole numbers : 42
C
SharpC
ode.org
10. OOPS Features…
Data Reusability – helps in saving developers time
You can use already created class to crate new one
Called inheritance
Already existing class is base class and new created isAlready existing class is base class and new created is
derived class
Base class members can be available in derived class and to
access them create object of derived class
Like from parent to child
C
SharpC
ode.org
11. class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
class CRectangle: public CPolygon {
public: int area ()
{ return (width * height); }
};
class CTriangle: public CPolygon {class CTriangle: public CPolygon {
public: int area ()
{ return (width * height / 2); } };
int main () {
CRectangle rect;
CTriangle trgl; rect.set_values (4,5);
trgl.set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}
C
SharpC
ode.org
12. OOPS Features…
In C++, overriding is a concept used in inheritance which involves a base class
implementation of a method.
Then in a subclass, you would make another implementation of the method. This is
overriding. Here is a simple example.
class Base
{{
public:
virtual void DoSomething() {x = x + 5;}
private:
int x;
};
class Derived : public Base
{
public:
virtual void DoSomething() { y = y + 5; Base::DoSomething(); }
private:
int y;
};
C
SharpC
ode.org