0% found this document useful (0 votes)
76 views10 pages

Classes and Objects: Definition of OOPS

The document discusses the key concepts of object-oriented programming (OOP). It defines OOP as a method of implementation where programs are organized as cooperative collections of objects that represent instances of classes within a hierarchy. The main concepts covered are: 1. Objects store data and send/receive messages at runtime, representing real-world entities. 2. Classes define types of objects and their shared properties and methods. 3. Inheritance allows classes to inherit attributes and behaviors from other classes in a hierarchy. 4. Polymorphism means classes can take on multiple forms through virtual functions that allow subclasses to override behaviors defined in parent classes.

Uploaded by

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

Classes and Objects: Definition of OOPS

The document discusses the key concepts of object-oriented programming (OOP). It defines OOP as a method of implementation where programs are organized as cooperative collections of objects that represent instances of classes within a hierarchy. The main concepts covered are: 1. Objects store data and send/receive messages at runtime, representing real-world entities. 2. Classes define types of objects and their shared properties and methods. 3. Inheritance allows classes to inherit attributes and behaviors from other classes in a hierarchy. 4. Polymorphism means classes can take on multiple forms through virtual functions that allow subclasses to override behaviors defined in parent classes.

Uploaded by

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

CLASSES AND OBJECTS

Characteristics of OOPS • Programs are divided into known objects • Builds the data and functions around these objects or
entities

Definition of OOPS:

OOP is a method of implementation in which programs are organized as cooperative collections of objects, each of which
represent an instance of some class and whose classes are all members of a hierarchy of classes united through the
property called inheritance.

Concepts of OOPS General concepts of OOPS comprises the following

1. Object 2. Class 3. Data abstraction 4. Inheritance 5. Polymorphism 6. Dynamic Binding 7. Message passing.

1. Object

Object is an entity that can store data and, send and receive messages. They are runtime entities, they may represent a
person, a place a bank account, a table of data or any item that the program must handle. It is an instance of a class.

2. Classes

A class is a collection of objects of similar type. Classes are user defined data types and behave like the built in types of a
programming language. For example mango, apple and orange are members of the class fruit. Then the statement FRUIT
MANGO; will create an object mango belonging to the class fruit. The syntax used to create an object is no different than
the syntax used to create an integer object in C. if fruit has been defined as a class, then the statement

fruit mango;

Will create an object mango belonging to the class fruit.

Object is an entity that can store data and send and receive messages. They are run time entities they may also represent
user-defined data.

3. Data abstraction and encapsulation:

4. Inheritance :

Types of Inheritance:

1. Single inheritance

2. Multiple inheritance

3. Multilevel inheritance

4. Hierarchical inheritance(refer Assignment)

5. Hybrid inheritance

1. SINGLE INHERITANCE:-

The new class can be derived from only one base class is called single

inheritance.

2.Multilevel Inheritance:-

The class A serves as a base class for the derived class B which in turn serves

as a base class for the derived class C. The chain ABC is known as inheritance path.

Class A

{
//body of base class A

};

Class B: public A

//body of intermediate base class B

};

Class C: public B

//body of derived class

3. MULTIPLE INHERITANCE:-

A class can inherit the attributes or properties of two or more classes that is a

new class can be derived from more than one base class is called multiple inheritance.

Here the class A , class B and class C serves as a base class for the derived class D

The derived class definition syntax is:

class d: visibility base-1, visibility base 2,…

body of class D;

};

Example:

#include<iostream.h> int sub1,sub2;

class A public:

{ void getmark(int y,int z)\

protected: {

int rollno; sub1=y;

public: sub2=z;

void getroll(int x) }

{ };

rollno=x; class C : public A, public B

} {

}; int total;

class B public:

{ void display()

protected: {
total=sub1+sub2; {

cout<<”roll C s;
no:”<<rollno<<”sub1:”<<sub1<<”sub2:”<<sub2;
s. getroll(435);
cout<<”total”<<total;
s.getmark(100,90);
}
s.display();
};
}
void main()

4. HIERARCHICAL INHERITANCE:-

The hierarchical inheritance structure is given below .This type is helpful when

We have class hierarchies. In this scheme the base class will include all the features

That are common to the subclasses.

#include<iostream.h>

#include<string.h>

class A

protected:

int x, y;

public:

void get ( )

cout<<”Enter two values”<<endl;

cin>> x>>y;

};

Class B: public A

Private:

int m;

Public:

Void add ( )

m= x + y;

cout<<”The Sum is “<<m;

};
class C : public A

Private:

int n;

public:

void mul( )

n= x * y;

cout << “The Product is “<<n;

};

Class D: public A

Private:

float l;

public:

void division( )

l = x / y;

cout <<”The Quotient is “<< l;

};

void main( )

B obj1; C .get( );

C obj2; C .mul( )

D obj3; D .get( );

B .get( ); D .division( );

B .add( ); }

5. HYBRID INHERITANCE:-

Hybrid inheritance = multiple inheritance + multi-level inheritance

The hybrid inheritance is defined as a combination of multilevel and multipleInheritances. This new class can be derived by
either multilevel or multiple method or both.

Example program

In this program the derived class (result) object will be inheriting the properties of both test class and sports class.
#include <iostream.h>

class student public:

{ void getscore (int a )

protected: {

int rollno; score=a;

public: }

void getroll (int x) };

{ class result: public test, public sports

rollno = x; {

} int total;

}; public:

class test: public student void display()

{ {

protected: total= sub1+sub2+score;

int sub1, sub2’ cout<<”rollno:”<<rollno<< “total:”<<”Score:”<<total;

public: }

void getmark (int y, int z) };

{ void main( )

sub1 = y; sub2 = z; {

} result S;

}; s.getroll(101);

class sports s.getmark(90,98);

{ s.getscore(2000);

protected: s. display( );

int score; }

VIRTUAL FUNCTION:

When we use the same function name in base and derived classes, the function in the base classes is declared as virtual
using keyword ‘virtual’ preceding its normal declaration. The member function that can be changed at runtime is called
virtual function.

Class classname

public:

.....

virtual returntype functionname (arguments)

{
.....

. . . . .}};

VIRTUAL BASE CLASS:-

The duplication of inherited members due to the multiple paths can be avoided

by making the common base class as virtual base class while declaring the direct or

intermediate base classes as shown below.

class A

------

------

};

class B1:virtual public A

-----

-----

};

class B2:public virtual A

-----

-----

};

class C:public B1,public B2

-----

-----

};

*******************************************************************************************************************************

5. Polymorphism: Polymorphism means the ability to take more than one form.

6. Dynamic binding: Dynamic binding(dispatch) means that a block of code executed with reference to a
procedure(method) call is determined at run time.

// C++ program to illustrate the concept of dynamic binding

#include <iostream>

using namespace std;

class B
{

public:

// Virtual function

virtual void f()

cout << "Base class function called.\n";

};

class D: public B

public:

void f()

cout << "Derived class function called.\n";

};

int main()

B base;

D derived;

B *basePtr = &base;

basePtr->f();

basePtr = &derived;

basePtr->f();

return 0;

7. message communication : An object oriented program consists of a set of objects that communicate with each other.
Objects communicate with one another by sending and receiving information much the same way as people pass
messages to one another. Objects have a life cycle. They can be created and destroyed. Communication with an object is
feasible as long as it is alive.

Inline function : One of the objective of usin g functions in a program is to save some memory space , which becomes
appreciable when a function is likely to be called many times.

To eliminate the cost of calls to small function C++ proposes a new feature called inline function.

An inline function is a function that is expanded in a line, this function keeps a request to compiler to give preference to
execute it as a request, but it does not makes a commands to give that preference to execute.

Inline function_header

function body

CONSTRUCTORS AND OVERLOADING :


CONSTRUCTOR :- A constructor is special member function whose task is to initialize the objects of its class. It is special
because its name is the same as the class name.

DESTRUCTOR:- It is used to destroy the objects that have been created by a constructor. Like a constructor, the destructor
is a member function whose name is the same as the class name but is preceded by a tilde.

SCOPE RESOLUTION OPERATOR:-

FRIEND CLASSES AND FRIEND FUNCTIONS:

FRIEND FUNCTION A function that has access to the private member of the class but is not itself a member of the class is
called friend functions.The general form is

friend data_type function_name( );

Friend function is preceded by the keyword ‘friend’.

#include<iostream.h> {

class result; //advance declaration int tot;

class test public:

{ friend void display(test,result); // friend function


declaration
int n1,n2;
};
public:
void display( test t, result r)
void getmark( )
{
{
r.tot= t.n1+t.n2; // private member of test class accessed
n1=45; using t object
n2=78; cout<<r. tot;
} }
friend void display( test, result); // friend function void main( )
declaration
{
};
test t;
class result
result r; display( t, r);

t.getmark( ); }

USES OF FRIEND FUNCTION:

1. Function operating on objects of two different classes.

2. Friend function can be used to increase the versatility of overloaded operators.

FRIEND CLASSES

Definition:

All the functions of another class to manipulate or use the private members of the friend class.

Example:

#include<iostream.h>

class two;

class one

private:

int x, y;

public: { cout<<”X is: “<<obj.x<<”Y is:”<<obj.y;

void setdata(int a, int b) cout<< “sum of one class data members”<< z;

{ }

x=a; y=b; };

} void main( )

friend class two; {

}; one obj1;

class two two obj2;

{ obj1. setdata( 45,65);

int z; obj2. addone(obj1);

public: obj2. display( obj1);

void addone(one obj) }

{ Run

z= obj. x + obj . y; X is: 45

} Y is: 65

void display(one obj ) sum of one class data members: 110

THIS POINTER:-
C++ uses a unique keyword called this to represent an object that invokes a member function. This is a pointer that points
to the object for which this function was called

You might also like