Aarya Pps
Aarya Pps
Enrolment number 1
(2411244019)
Object-oriented programming is the most recent concept among programming paradigms and
still means different things to different people.
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.
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.
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++.
#include
<iostream> using
namespace std;
int main()
{
cout<<"Hello World";
return 0;
}
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;
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;
}
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
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:
Code:
#include <iostream>
using namespace std;
void add()
{
int a[2][2],b[2][2],add[2][2],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:
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:
Output:
Output:
#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()
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 :";
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:
Output:
#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;
Output: