0% found this document useful (0 votes)
15 views25 pages

Aarya Pps

The document provides an overview of Object-Oriented Programming (OOP) concepts, emphasizing its advantages over procedural programming, such as data encapsulation, inheritance, and polymorphism. It explains key OOP concepts including classes, objects, data abstraction, and the benefits of using OOP for software development. Additionally, it introduces C++ as an object-oriented programming language, detailing its features, applications, and differences from C.

Uploaded by

reds1sun2
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)
15 views25 pages

Aarya Pps

The document provides an overview of Object-Oriented Programming (OOP) concepts, emphasizing its advantages over procedural programming, such as data encapsulation, inheritance, and polymorphism. It explains key OOP concepts including classes, objects, data abstraction, and the benefits of using OOP for software development. Additionally, it introduces C++ as an object-oriented programming language, detailing its features, applications, and differences from C.

Uploaded by

reds1sun2
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/ 25

PRACTICAL- 1

AIM: LEARN CONCEPTS OF OOP


1. Explain Object Oriented Paradigm with figure.
The major motivating factor in the invention of object-oriented approach is to remove some
of the flaws encountered in the procedural approach. OOP treats data as a critical element in
the program development and does not allow it to flow freely around the system. It ties data
more closely to the function that operate on it, and protects it from accidental modification
from outside function. OOP allows decomposition of a problem into a number of entities
called objects and then builds data and function around these objects. The organization of
data and function in object-oriented programs is shown in fig. The data of an object can be
accessed only by the function associated with that object. However, function of one object
can access the function of other objects.

Organization of data and function in OOP


Some of the features of object-oriented programming are:
 Emphasis is on data rather than procedure.
 Programs are divided into what are known as objects.
 Data structures are designed such that they characterize the objects.
 Functions that operate on the data of an object are ties together in the data structure.
 Data is hidden and cannot be accessed by external function.
Objects may communicate with each other through function.

New data and functions can be easily added whenever necessary.

Follows bottom-up approach in program design.

Enrolment number 1
(2411244019)
Object-oriented programming is the most recent concept among programming paradigms and
still means different things to different people.

2. Explain basic Concepts of OOP with example


1. Class
2. Object
3. Data Encapsulation
4. Data Abstraction
5. Data Hiding
6. Inheritance
7. Polymorphism
It is necessary to understand some of the concepts used extensively in object-oriented
programming. These include:
 Objects
 Classes
 Data abstraction and encapsulation
 Inheritance
 Polymorphism
 Dynamic binding
 Message passing
We shall discuss these concepts in some detail in this section.
Objects
Objects are the basic run time entities in an object-oriented system. They may represent a
person, a place, a
bank account, a table of data or any item that the program has to handle. They may also
represent user-defined data such as vectors, time and lists. Programming problem is analysed
in term of objects and the nature of communication between them. Program objects should be
chosen such that they match closely with the real-world objects. Objects take up space in the
memory and have an associated address like a record in Pascal, or a structure in c.
When a program is executed, the objects interact by sending messages to one another. For
example, if “customer” and “account” are to object in a program, then the customer object
may send a message to the count object requesting for the bank balance. Each object contain
data, and code to manipulate data. Objects can interact without having to know details of each
other’s

Enrolment number 2
(2411244019)
data or code. It is a sufficient to know the type of message accepted, and the type of response
returned by the objects. Although different author
represent them differently fig shows two notations that are popularly used in object-oriented
analysis and design.
Fig. representing an object
Classes
We just mentioned that objects contain data, and code to manipulate that data. The entire set
of data and code of an object can be made a user-defined data type with the help of class. In
fact, objects are variables of the type class. Once a class has been defined, we can create any
number of objects belonging to that class. Each object is associated with the data of type class
with which they are created. A class is thus a collection of objects similar types. For
examples, Mango, Apple and orange members of class fruit. Classes are user-defined that
types and behave like the built-in types of a programming language. The syntax used to
create an object is not different then 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.
Data Abstraction and Encapsulation
The wrapping up of data and function into a single unit (called class) is known as
encapsulation. Data and encapsulation are the most striking feature of a class. The data is not
accessible to the outside world, and only those functions which are wrapped in the class can
access it. These functions provide the interface between the object’s data and the program.
This insulation of the data from direct access by the program is called data hiding or
information hiding.
Abstraction refers to the act of representing essential features without including the
background details or explanation. Classes use the concept of abstraction and are defined as a
list of abstract attributes such as size, weight, and cost, and function operate on these
attributes. They encapsulate all the essential properties of the object that are to be created.
OBJECTS: STUDENT DATA
Name
Date-of-birth Marks

FUNCTIONS

Total

Average

Display

Enrolment number 3
(2411244019)
The attributes are sometime called data members because they hold information. The
functions that operate on these data are sometimes called methods or member function.

Inheritance
Inheritance is the process by which objects of one class acquired the properties of objects of
another classes. It supports the concept of hierarchical classification. For example, the bird”
robin‟ is a part of class „flying bird‟ which is again a part of the class „bird‟. The principal
behind this sort of division is that each derived class shares common characteristics with the
class from which it is derived as illustrated in fig 1.6.
In OOP, the concept of inheritance provides the idea of reusability. This means that we can
add additional features to an existing class without modifying it. This is possible by deriving
a new class from the existing one. The new class will have the combined feature of both the
classes. The real appeal and power of the inheritance mechanism is that it
Allows the programmer to reuse a class i.e., almost, but not exactly, what he wants, and to
tailor the class in such a way that it does not introduce any undesirable side-effects into the
rest of classes.

Polymorphism
Polymorphism is another important OOP concept. Polymorphism, a Greek term, means the
ability to take more than on form. An operation may exhibit different behaviour is different
instances. The behaviour depends upon the types of data used in the operation. For example,
consider the operation of addition. For two numbers, the operation will generate a sum. If the
operands are strings, then the operation would produce a third string by concatenation. The
process of making an operator to exhibit different behaviours in different instances is known
as operator overloading. Illustrates that a single function name can be used to handle different
number and different types of argument. This is something similar to a particular word
having several different meanings depending upon the context.

Enrolment number 4
(2411244019)
Using single function name to perform different type of task is known as function overloading.

3. State various benefits of OOP


OOP offers several benefits to both the program designer and the user. Object-Orientation
contributes to the solution of many problems associated with the development and quality of
software products. The new technology promises greater programmer productivity, better
quality of software and lesser maintenance cost. The principal advantages are:
 Through inheritance, we can eliminate redundant code extend the use of existing
Classes.
 We can build programs from the standard working modules that communicate with
one another, rather than having to start writing the code from scratch. This leads to
saving of development time and higher productivity.
 The principle of data hiding helps the programmer to build secure program that
cannot be invaded by code in other parts of a programs.
 It is possible to have multiple instances of an object to co-exist without any
interference.
 It is possible to map object in the problem domain to those in the program.
 It is easy to partition the work in a project based on objects.
 The data-cantered design approach enables us to
capture more detail of a model can implemental form.
 Object-oriented system can be easily upgraded from small to large system.
 Message passing techniques for communication between objects make to interface
descriptions with external ystems much simpler.
 Software complexity can be easily managed.
While it is possible to incorporate all these features in an object-oriented system, their
importance depends on the type of the project and the preference of the programmer. There
are a number of issues that need to be tackled to reap some of the benefits stated above. For
instance, object libraries must be available for reuse. The technology is still developing and
current product may be superseded quickly. Strict controls and protocols need to be
developed if reuse is not to be compromised.

Enrolment number 5
(2411244019)
PRACTICAL - 2
AIM : CONCEPTS OF OOP USING C++

1. What is C++?
 The language C++ was developed by Bjarne strousstrup in 1979 at bell lab.
 This is popularly known as object-oriented programming language. This language is
compiled.
 C++ is rich, robust programming language.
 The most important facilities of c++ are classes, inheritance, function overloading and
operator overloading.

2. Give the Applications of C++.


 C++ is versatile language for handling very large programs.
 Since c++ allows up to create hierarchy related objects, we can build special object-
oriented libraries which can be used by many programmers.
 C++ is able to map real world problem properly.
 C++ programs are easily maintainable and expandable.
 It is expected that c++ will replace c as a general-purpose language in near future.
 Application is: photoshop, illustrator, acrobat.

3. State and explain differences between C & C++.


C C++

C is procedure-oriented language. C++is object-oriented language.

C makes used of top –down approach of C++ makes used of bottom-up approach of problem
problem solving. solving.
The input and output are done using scanf T he input and output are done using cin and cout
and printf statement. statement.
The I/O operations are supported by stdio.h The I/O operations are supported by iostream.h
header file. header
file.
C does not support inheritance, C++ supports inheritance, polymorphism,class and
polymorphism, class and object object concepts.
concepts.
The datatype specifier or format specifies The format specifier is not required in cin and cout
is required in printf and scanf function. function.

Enrolment number 6
(2411244019)
4. Draw and explain the structure of C++.

 Here, first line is comment. It is non executable


portion. The second line is header file
iostream.
 Next is void main () function. The void main ()
 Function. which execution of c++ program starts. the
next line is cout which represents what message to be printed on the output screen.
 Simple C++ Program Let us begin with a simple example
of a C++ program that prints a string on the screen.

#include
<iostream> using
namespace std;
int main()
{
cout<<"Hello World";
return 0;
}

5. Explain insertion (<<) and extraction (>>) operators of C++.


<<, is also known as extraction operator. It is normally used to take a value for a
variable through user.
Syntax: cin>>variable name;
>>, is also known as insertion operator. It is normally used to display the given value
of a variable or a message.
Syntax: cout<<variable name;

Enrolment number 7
(2411244019)
6. What are manipulators? Explain endl and setw with example.

 Manipulators are nothing but operators used in c++ that is used for formatting the outputs or in
other words manipulating data in the way the programmer wants to display it
 The most commonly used manipulators are endl and setw.
endl manipulator:
This manipulator does the same functionality as the “\n “new line character does. Insert a new
line character
Example:
cout<<"Hello"<<endl;
cout<<"World"<<endl;
Output:
Hello
World
setw manipulator:
This all manipulators are present in the header file
“iomanip”.
It is used to set the field width of the output on the output device.
Syntax: setw(number_of _colulmns)
Example:
#include
<iomanip>
#include
<iostream> using
namespace std;
int main()
{
cout<<setw(20)<<"hello"<<
endl; return 0;
Output:
Hello
7. W.A.P. to show the effects of manipulator
endl and setw.
setw():
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
cout<<setw(20)<<"hello"<<endl;
return 0;

Enrolment number (2201202025) 8


}
Output:
Hello
endl:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello"<<endl;
cout<<"World"<<endl;
return 0;
}
Output:
Hello
World
8. What is type conversion? Explain with
example.
There are two types of type conversion
 Implicit Type Conversion
 Explicit Type Conversion

I. Implicit Type Conversion

It is done by the compiler on its own, without any external trigger from the user.
Example:
#include <iostream>
using namespace std;
int main()
{
int a=10;
char b='a';
cout<<a+b;
return 0;
}
Output:
107
Here the output automatically got converted into integer type without any external interference.
II. Explicit Type Conversion

Here the user can typecast the result to make it of a particular data type.
Example:
#include <iostream>
using namespace std;
int main()
{
int a=-17;
char b='a';
char c=a+b;
cout<<c;
return 0;
}

Enrolment number (2201202025) 9


Output:
P
Here the user converted the result to a character.
9. W.A.P. to show the effect of type
conversion.
#include <iostream>
using namespace std;
int main()
{
int a=-17;
char b='a';
char c=a+b;
cout<<c;
return 0;
}
Output:
P
10. What is dynamic initialization? Explain
with example.
• The additional features of c++ are to initialize the
variable at runtime is said as a dynamic initialization.
• Whenever a program is written. Whenever a variable is required then it is declared.
• A dynamic initialization is extensively used in
extensively used in object-oriented programming.
For example:
float x=z+y; //Initializing z dynamically at runtime

In c language, variable is declared first and then used in program and it is called statis initialization.
For example:
float x,z,y;
11. W.A.P. to check the effect of dynamic
initialization.
#include <iostream>
using namespace std;
int main()
{
float z=10,y=20;
float x=z+y;
cout<<"X: "<<x<<endl;
return 0;
}
Output:
X: 30
12. Explain reference variable with example.
Reference Variable
When a variable is declared as a reference, it becomes an alternative name for an existing variable. A variable can be
declared as a reference by putting ‘&’ in the declaration.
int a=0;
int& ref=a;
13. W.A.P. that will show the effect of

Enrolment number (2201202025) 10


reference variable.
#include <iostream>
using namespace std;
int main()
{
int a=0;
int& ref=a;
ref=10;
cout<<"A: "<<a<<endl;
return 0;
}
Output:
A: 10
14. Write a function using variables as
arguments to swap the values of a pair of
integers using
call by value, call by address, and call by reference.
Call by value
#include <iostream>
using namespace std;
void swap(int a,int b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int x=10,y=20;
swap(x,y);
cout<<"X: "<<x<<endl;
cout<<"Y: "<<y<<endl;
return 0;
}
Output:
X: 10
Y: 20
Call by address
#include <iostream>
using namespace std;
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{

Enrolment number (2201202025) 11


int x=10,y=20;
swap(&x,&y);
cout<<"X: "<<x<<endl;
cout<<"Y: "<<y<<endl;
return 0;
}
Output:
X:20
Y:10
Call by reference
#include <iostream>
using namespace std;
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int x=10,y=20;
swap(x,y);
cout<<"X: "<<x<<endl;
cout<<"Y: "<<y<<endl;
return 0;
}
Output:
X:20
Y:10
15. Explain the working of scope resolution
operator (::) with example.
The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is same as local variable
name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the
class and used to access the static variables of class.
#include <iostream>
using namespace std;
int a = 10;
int main()
{
int a = 20;
cout << "Global A: " <<::a<<endl;
cout << "Local A: " <<a<<endl;
return 0;
}
Output:
Global A: 10
Local A: 20

Enrolment number (2201202025) 12


DEFAULT ARGUMENTS AND FUNCTION OVERLOADING
1. WAP to print following using default arguments repeated character using different times
using different arguments in the parameter.

Code:
#include <iostream>
using namespace std;
void repeatchar(char c='-' , int n=45)
{
for (int i = 0; i < n; i++)
{
cout<<c;
}
cout<<endl;
}
int main()
{
repeatchar();
repeatchar('/');
repeatchar('<',20);
return 0;
}
Output:

2. WAP to add two matrices using default argument.

Code:
#include <iostream>
using namespace std;
void add()
{
int a[2][2],b[2][2],add[2][2],i,j;

Enrolment number (2201202025) 13


cout<<"Matrix A : "<<endl;
for ( i = 0; i < 2; i++)
{
for ( j = 0; j < 2; j++)
{
cin>>a[i][j];
}
}
cout<<"Matrix B : "<<endl;
for ( i = 0; i < 2; i++)
{
for ( j = 0; j < 2; j++)
{
cin>>b[i][j];
}

}
cout<<"Addition of A & B : "<<endl;
for ( i = 0; i < 2; i++)
{
for ( j = 0; j < 2; j++)
{
add[i][j]=a[i][j]+b[i][j];
}
}
for ( i = 0; i < 2; i++)
{
for ( j = 0; j < 2; j++)
{
cout<<"\t"<<add[i][j];
}
cout<<endl;
}
}
int main()
{
add();
return 0;
}
Output:

Enrolment number (2201202025) 14


3. WA function called zerosmaller() uses two arguments. Use return by reference to
the function concept and set a smaller value to 0.

Code:
#include <iostream>
using namespace std;
void zerosmaller(int &a, int &b)
{
if (a>b)
{
b=0;
}
else
{
a=0;
}
}
int main()
{
int c,d;
cout<<"C : ";
cin>>c;
cout<<"D : ";
cin>>d;
zerosmaller(c,d);
cout<<"C : "<<c<<endl;
cout<<"D : "<<d<<endl;
return 0;
}
Output:

Enrolment number (2201202025) 15


4. WAP that prints various types of data using function overloading.
Code:
#include <iostream>
using namespace std;
void f1(int a)
{
for (int i = 0; i <=5; i++)
{
cout<<a<<" ";
}
}
void f1(float b)
{
for (int i = 0; i <=5; i++)
{
cout<<b<<" ";
}
}
void f1(char c)
{
for (int i = 0; i <=5; i++)
{
cout<<c<<" ";
}
}
int main()
{
int d;
float e;
char f;
cout<<"Integer : ";
cin>>d;
cout<<"Float : ";
cin>>e ;
cout<<"Character : ";
cin>>f;
f1(d);
cout<<endl;
f1(e);
cout<<endl;
f1(f);
cout<<endl;

Enrolment number (2201202025) 16


return 0;
}

Output:

5. WAP that prints a character on screen using function overloading.


Code:
#include <iostream>
using namespace std;
void f1(char a)
{
for (int i = 0; i < 5; i++)
{
cout<<a<<" ";
}
}
void f1(char b[20])
{
for (int i = 0; i < 5; i++)
{
cout<<b<<" ";
}
}
int main()
{
char c,d[20];
cout<<"Character : ";
cin>>c;
cout<<"String : ";
cin>>d;
f1(c);
cout<<endl;
f1(d);
return 0;
}
Output:

Enrolment number (2201202025) 17


6. WA function power() to raise a number m to power n. The function takes a double value
for m and int value for n, and returns the value (results) correctly. Use default argument 2 for n
and m to make a function to calculate squares when argument is not passed. WAP for the same.
Code:
#include <iostream>
using namespace std;
double power(double m,int n=2)
{
double a=1;
for (int i = 1; i <=n; i++)
{
a=a*m;
}
return a;
}
int main()
{
double b,ans;
int c;
cout<<"Number : ";
cin>>b;
cout<<"Exponent : ";
cin>>c;
ans=power(b,c);
cout<<"Answer : "<<ans<<endl;
ans=power(b);
cout<<"Answer (with default parameters) : "<<ans<<endl;
return 0;
}

Output:

7. Write overloaded functions to convert ascii to int and ascii to float.


Code:
#include <iostream>
using namespace std;
void f1(char c,int i=0)
{
i=c;
cout<<"1. "<<i<<endl;
}
void f1(char c,float f=0.0)
{

Enrolment number (2201202025) 18


f=c;
cout<<"2. "<<f<<endl;
}
int main()
{
float f=0;
int i=0;
char c;
cout<<"Enter: ";
cin>>c;
f1(c,i);
f1(c,f);
return 0;
}
Output:

Enrolment number (2201202025) 19


CLASS AND OBJECTS
1. Create a class player with the following data members name, age, runs, hi, lo, tests, avg.
Write member functions for each of the following
a. To get the data.
b. To display the data.
c. To calculate the average of the player.
Code:

#include<iostream.h>
#include<conio.h>
class player
{
char name[20];
int age,runs,high,low,test;
float avg;
public:
void getdata();
void putdata();
void calc_average();
};
void player::getdata()
{
cout<<"Enter the player name :";
cin>>name;
cout<<"Enter the age of player :";
cin>>age;
cout<<"Enter the runs of player :";
cin>>runs;
cout<<"Enter the highest score,lowest score and test played by the
player :";
cin>>high>>low>>test;
}
void player::putdata()
{
cout<<"Name :"<<name<<endl;
cout<<"Age :"<<age<<endl;
cout<<"Runs :"<<runs<<endl;
cout<<"Highest Score :"<<high<<endl;
cout<<"Lowest Score :"<<low<<endl;
cout<<"Test Played :"<<test<<endl;
cout<<"Average of player :"<<avg<<endl;
}
void player::calc_average()
{
avg=(float)runs/test;
}
void main()

Enrolment number (2201202025) 20


{
clrscr();
player p1;
p1.getdata();
p1.calc_average();
p1.putdata();
getch();

Output:

2. Create a class item with the following data members item code, cost, qty, total_price. Write
member functions for each of the following:
a. To get the data.
b. To display the data.
c. To calculate the total price of the item.
Code:

#include<iostream.h>
#include<conio.h>
class item
{
int itemcode,quantity;
float cost,price;
public:
void getdata();
void putdata();
void price();
};
void item::getdata()
{
cout<<"Enter the code of item :";
cin>>itemcode;
cout<<"Enter the quantity of item :";

Enrolment number (2201202025) 21


cin>>quantity;
cout<<"Enter the cost of item :";
cin>>cost;
}
void item::putdata()
{
cout<<"item code :"<<itemcode<<endl;
cout<<"Quantity :"<<quantity<<endl;
cout<<"Cost :"<<cost<<endl;
cout<<"Total price :"<<price<<endl;
}
void item::price()
{
price=quantity*cost;
}
void main()
{
clrscr();
item i1;
i1.getdata();
i1.total_price();
i1.putdata();
getch();
}

Output:

3. Create a class book with following data members book name, author name, rate, qty Write
member functions for each of the following
a. To get the data.
b. To display the data.
c. To calculate the total price of the book.
Code:
#include<iostream.h>
#include<conio.h>
class book
{
int rate,quantity,price;
char bookname[30],author[30];
public:

Enrolment number (2201202025) 22


void getdata();
void putdata();
void total();
};
void book::getdata();
{
cout<<"Enter name of the book :";
cin>>bookname;
cout<<"Enter the Author name:";
cin>>author;
cout<<"Enter Quantity of books :";
cin>>quantity;
cout<<"Enter Rate :";
cin>>rate;
}
void book::total()
{
price=rate*quantity;
}
void book::putdata()
{
cout<<"Name of the book :"<<bookname<<endl;
cout<<"Quantity of book :"<<quantity<<endl;
cout<<"Author of book :"<<author<<endl;
cout<<"Rate of the book :"<<rate<<endl;
cout<<"Price of the book :"<<price<<endl;
}
void main()
{
clrscr();
book b;
b.getdata();
b.total();
b.putdata();
getch();
}

Output:

Enrolment number (2201202025) 23


4. Create a time with following data members int h,m,s Write member functions for each of
the following.
a. To get the data in number of seconds.
b. To set the data in number of seconds.
c. To display the data.
d. To convert seconds into h, m, s.
Code:

#include<iostream.h>
#include<conio.h>
class time
{
int hr,min,sec;
public:
void getdata();
void putdata();
void setdata();
void display();
};
void time::getdata()
{
cout<<"Enter time in seconds";
cin>>sec;
}
void time::putdata()
{
cout<<"No of seconds only :"<<sec<<endl;
}
void time::setdata()
{
int n;
n=sec/3600;
hr=n;
n=hr*3600;
sec=sec-n;
n=sec/60;
min=n;
n=min*60;
sec=sec-n;

Enrolment number (2201202025) 24


}
void time::display()
{
cout<<hr<<" "<<min<<" "<<sec;
}
void main()
{
clrscr();
time t1;
t1.getdata();
t1.putdata();
t1.setdata();
t1.display();
getch();
}

Output:

Enrolment number (2201202025) 25

You might also like