OOP's Basic to Advanced Topics Part 1 - Interview Preparation

Object Oriented Programming Structure

  • Programming paradigm - Way of thinking / approaching a problem
  • C++ is a multi-paradigm language
  • Advantages of OOPS
  • Improved software-development productivity
  • Modular, Extensible, Reuse
  • Improved software maintainability
  • Faster development
  • Lower cost of development
  • Higher-quality software
  • Disadvantages of OOPS
  • Steep learning curve
  • Larger program size
  • Slower programs
  • Not suitable for all types of problems

Basis of Object Oriented Programming

  • View the problem world as a set of objects and communication between the objects
  • Entire world around as can be thought as a set of objects
  • Objects have characteristics and they can perform some functions
  • Similar objects may be grouped together

Features of Object Oriented Paradigm

image

OOPS Concept Definitions

Now, let us discuss some of the main features of Object Oriented Programming which you will be using in C++(technically).

  • Objects
  • Classes
  • Abstraction
  • Encapsulation
  • Inheritance
  • Overloading
  • Exception Handling

Class and Objects

Class
It is similar to structures in C language. Class can also be defined as user defined data type but it also contains functions in it. So, class is basically a blueprint for object. It declare & defines what data variables the object will have and what operations can be performed on the class's object.

Objects
Objects are the basic unit of OOP. They are instances of class, which have data members and uses various member functions to perform tasks.

Abstraction
• Abstraction refers to showing only the essential features of the application and hiding the details. In C++, classes can provide methods to the outside world to access & use the data variables, keeping the variables hidden from direct access, or classes can even declare everything accessible to everyone, or maybe just to the classes inheriting it. This can be done using access specifiers.

Encapsulation
• Encapsulation is an Object Oriented Programming concept that binds together the data +functions that manipulate the data, and that keeps both safe from outside interference and misuse.

Polymorphism
• It is a feature, which lets us create functions with same name but different arguments, which will perform different actions. That means, functions with same name, but functioning in different ways. Or, it also allows us to redefine a function to provide it with a completely new definition. You will learn how to do this in details soon in coming lessons.

Class
• Depicts the set of similar objects as mentioned in the previous example.
• It distinguishes one type of object from another type
• A Class is an User defined data type
• A class comprised of attributes and functions.
• Attributes defines the properties of the object and function describes “What an object is capable of doing”
• The class is like a blue print of a house and it indicates how the data and functions are used by the objects.

Class is generalization of objects where data and functions are grouped
image
image
Objects
• In programming perspective, instance of a class
• we consider the real-world, we can find many objects around us, cars, dogs, person, etc. All these objects have a state and a behaviour.
image

Class Declaration

class class_name
{ 
	private: 
		members1; 
	protected:
		members2; 
	public:
		members3;
};

Note : Members, that can be either data or function declarations

Access specifiers

• Private members of a class are accessible only from within other members of the same class. You cannot access it outside of the class.
• Public members are accessible from anywhere where the object is visible.
• Protected members are accessible from members of their same class and also from members of their derived classes.

class Example
{ 
	private: 
		double radius;
	public: 
		void setRadius(double r)
		{
			radius = r;
		}
		double getArea()
		{ 
			return 3.14 * radius * radius; 
		} 
};

Note :When class is defined, only the specification for the object is defined; no memory or storage is allocated.

Object Declaration And Accessing Members

• To use the data and access functions defined in the class, you need to create objects.
• Syntax to Define Object in C++ className objectVariableName; Example : Circle c1, c2;
• Accessing Class Members
You can access the data members and member functions by using a . (dot) operator.
Example : c1.setRadius(2.5);
Accessing the Data Members (without Functions) – Example 1

#include <iostream> 
using namespace std; 
class person
{ 
	public: 
		string name;
		int age;
};	
int main()
{
	person obj;	
	cout<<"Enter the Name: ";
	cin>>obj.name;
	cout<<"Enter the Number :";
	cin>>obj.age;
	cout << obj.name << ": " << obj.age<< endl;
	return 0;
}

Accessing the Data Members (with Functions) –
Example 2

#include <iostream>
using namespace std;
 class Circle //specify a class
{
    private :
        double radius; 
    public:
        void setRadius(double r)
        {
            radius = r;
        }
        double getArea()
        {
            return 3.14 * radius * radius;
        }
};
int main()
{
    Circle c1; //define object of class circle

//call member function to initialize radius

    c1.setRadius(2.5); 

//display area of circle object

    cout<<c1.getArea(); 

    return 0;
}

Accessing private and public the Data Members –
Example 3

#include <iostream>
 using namespace std;
 class Box {
private:
      double width;
   public:
      double length;
      double getWidth(void) {
   return width ;
}
      void setWidth( double wid ) {
   width = wid;
}
};
int main( ) {
   Box  box;
   // OK: because length is public
   box.length = 10.0; 
    cout << "Length of box : " << box.length <<endl; 
   // set box width without member function
   // box.width = 10.0; // Error: because width is private
   box.setWidth(10.0);  // Use member function to set it.
   cout << "Width of box : " << box.getWidth() <<endl;
    return 0;
}

Defining Member function of class (outside the class definition) – Example4

When we define a function outside the class we cannot reference them (directly) outside of the class. In order to reference these, we use the scope resolution operator, :: (double colon).

#include <iostream>
using namespace std;
 
class Circle //specify a class
{
    private :
        double radius; //class data members
    public:
        void setRadius(double r);
        double getArea(); //member function to return area
};
 
void Circle :: setRadius(double r)
{
    radius = r;
}
double Circle :: getArea()
{
    return 3.14 * radius * radius;
}
 
int main()
{
    Circle c1; //define object of class circle
    c1.setRadius(2.5); //call member function to initialize radius
    cout<<c1.getArea(); //display area of circle object
    return 0;
}

ARRAY OF OBJECTS – Example 5

#include <iostream>
using namespace std;
class employee
{
    private:
	    string name;
        int empno;
		float salary;
	public:
	    void getdetails( )
		{
             cout << "\n Enter the name:";
			 cin >> name;
			 cout << "\n Enter the empno:";
			 cin >> empno;
			 cout << "\n Enter the salary:";
			 cin >> salary;
		}
		void printdetails()
		{
		    cout << "Employee Name :" << name <<endl<<"Emp Number:" <<empno<<endl<<"Salary"<<salary<<endl;
		}
};
int main()
{
    employee e[10];
    int i=0;
    char choice;
    do{
	    cout << "Enter the Employee Number::" << i+1;
		e[i++].getdetails();
		cout << "Enter another (y/n)?: " ;
		cin >>choice;
    } while ( choice != 'n' );
    for (int j=0; j<i; j++)
    {
        cout << "\nEmployee Number is:: " << j+1<<endl;
		e[j].printdetails();
   }
}
Comments (1)