Sie sind auf Seite 1von 12

LAB MANUAL

IT-108 Object Oriented Programming


Implementation of C++ programming problems

AIM:
Aim of this Lab is to practice and solve logical & mathematical problems using C++ language.
Strengthen knowledge of a programming from procedural to Object Oriented Technique. Design
and develop solutions to intermediate level problems using the C++ language.

Objectives:
The objective of this course is to enable students that they can organize of data objects and their
interaction using Object Oriented Technique which is closer to the real world representation of
objects. Offer the modern programming language C++ that shall help the students to implement the
various concept of object orientation practically. The students will be able to write program in the
object oriented technology with the usage of C++. This course will also prepare students with
the necessary programming background for Data Structures using C/C++ and Java programming
courses.

DESCRIPTION OF
SCHEDULE:
Week PRACTICAL

REVISION OF CONCEPTS
SIMPLE PROBLEMS
1. Write a C++ program to display "Asalam Alykum!" on the screen.
2. Write a C++ program to display Your Name, Address and City in different lines
3. Write a C++ program to find the area of a circle using the
formula: Area = PI * r2
4. Write a C++ program to find the area and volume of
sphere. Formulas are: Area = 4*PI*R*R Volume =
4/3*PI*R*R*R.
5. Write a C++ program to print the multiply value of two
accepted numbers
6. Write a C++ program to convert centigrade into
Fahrenheit. Formula: C= (F-32)/1.8.
7. Write an C++ program that declares 5 integers, determines
1
and prints the largest and smallest in the group.
8. Write a C++ program that declares two integers, determines
whether the first is a multiple of the second and print the
result. [Hint: Use the remainder operator.]
PROBLEMS
1. Write a program in C++ to display your name, Branch, Year on the
Computer screen without using classes and object. All information
should be displayed in the separate line.
Use Function Overloading in following problems w.r.t integer and float values.
Function will have same name but type of arguments will differ.
2. Write a menu driven program in C++ to perform all basic arithmetic
operation addition, subtraction, multiplication, and division of two
given values. Program receives two values and required operation to be
performed from the keyboard and display particular result of the
required operation.
3. Write a menu driven program in C++ that receives 4 digit integer value and
perform following operations (a) Reverse of that no. (b) Sum of number
2 with its reverse (c). sum of alternative digits(e.g. 1 digit+3 digit , 2 digit+4
digit)
4. Write a menu driven program in C++ to receive integer number
and convert equivalent binary, octal, hexadecimal number.
5. Write a menu driven program in C++ to perform all basic arithmetic
operation addition, subtraction, multiplication, and division of two
given values using function and switch case. Program receives two
values and required operation to be performed from the keyboard and
display particular result of the required operation.
Classes & Objects
Specifying a class, creating class objects, accessing class members, access
specifiers: public, private, and protected, classes, objects and memory, static
members, the const keyword and classes, the static objects, empty classes,
nested classes, local classes, abstract classes
List of programs:
1. Write a program in C++ to display mark sheet. Of the student. Define a class
Student that contains data members to store student information line name, Branch,
semester, marks in 6 different subjects, etc. Declare some member functions to get
this information from the key board, to calculate result and to display all gathered
information on to the computer screen in proper format.
2. Define a class employee. Include the following members:
Data Members:
a. Name of the
employee b. Age
of the employee
Member
Functions:
a. To get the name and age of the employee

b. To display the name and age of the employee.


3 3. Define a class BankAccount to represent a bank account. Include the following
Data Members:
1. Name of the depositor
2. Account Number
3. Type of account
4. Balance amount in the account
Member Functions:
1. To assign initial value
2. To deposit an amount
3. To withdraw an amount afer checking

4. Define a class employee having data members as emp_code, dept_code, age,


basic, DA, HRA and three member functions as getdata(), putdata(), calculatesalary()
to get, display all the values of data members and calculate the total salary by adding
basic, DA, HRA. Write this program for 10(ten) employees using an array of objects.
BANKING OPERATION USING CLASS AND OBJECTS
Algorithm

Step 1: Create an object b of the class bank Account.

4 Step 2: Read the number of records.

Step 3: call the init member function of the class bank account through the object
created.

Step 4: Assign 500 to the balance in the records.

Step 5: Display a menu with the following options create deposit withdraw

display.

Step 6: Read the choice.

Step 7: If the choice is create then call the create member function using the
object.

Step 8: prompt and read the user’s name, account number and account type for all

the n records.

Step 9: If the choice is deposit call the deposit member function using the function.

Step 10: Read the account number.

Step 11: Check whether this account number is already exiting in the list. If true

Step 12: Read the amount to be deposited.

Step 13: Update the balance by adding the amount deposited to the balance.

Step 14: Display the balance.

Step 15: If the choice is withdraw, call the withdraw member function.

Step 16: Using object Read the account number.

Step 17: Check the account number existing in the list

then.

Step 18: Read the amount to be withdrawn.

Step 19: Retrieve the amount from balance.

Step 20: If the balance is less than 500 then

Step 21: Calculate balance + withdraw amount-500.

Step 22: Assign 500 to the balance.

Step 23: Display the withdrawn amount.

Step 24: If the choice is display, invoke the display member function using the
Object.

Step 25: Read the account number.

Step 26: If this account number is existing in the list then.

Step 27: Display the details such as name, account number, account type and
balance in the given account number.
INVENTORY OF BOOK USING CONSTRUCTOR & DESTRUCTOR
Algorithm

Step 1: Create an object for the class book.

Step 2: Declare the pointer variable for author, title, and publisher and the
Variable for price and stock position.

Step 3: Read the number of records.

Step 4:Display a menu with the following choice create ,buybook,


transaction and display.

Step 5:Using switch case execute the statements corresponding to the


choice.

Step 6:If the choice is create, read the title, author, publishes, price
and stock

position and pass it to the constructor of the book class.

Step 7;Find the string length of each of the pointer variables


dynamically.

5 Step 8:If the choice is buy books, read the title, author, stock from
the user and check these with the array already created.

Step 9:If the author name and title matches then display the
message”:Available”
and read the number of copies.

Step 10:Decrement the stock position by 1 and display the mount


to be paid. Increment successful transaction by 1. Else display “NOT
success” and increment the unsuccessful transaction by 1.

Step 11:If the transaction; display the variables, successful


transaction and unsuccessful transaction.

Step 12:If the choice is display, then display all the details such as title, author,
price, publishes and stock position.

1. Create a class called Employee that includes three pieces of


information as instance variables – a first name (typ e String), a last name
(type String) and a monthly salary (double)
2. Create a constructor in above class to initialize the three instance variables.
Provide a get method for each instance variable..
3. Create two employee objects and display each object’s yearly salary.
4. Give each employee a 10% raise and display each Employee’s yearly salary again..
5. Write C++ program to create five object of book, get information
of book using getdata() function including name, price, publication and
author.
6. Write search() function to search a specified book, if book is search return the
6
complete information of book and print the information of book using putdata()
function.
Inheritance
Introduction, defining derived classes, forms of inheritance, ambiguity in
multiple and multi-path inheritance, virtual base class, overriding member
functions, order of execution of constructors and destructors.

AIM of this module


 describing the information contained within a class hierarchy
 Creating Derived Classes, Exploiting power of Inheritance through Sub
Classes

1. Create a class A with some private data members and some public member
function, now create a derived class B, that inherits A and having some data
members and member functions it’s own, in main( ) function access attributes
of base class with the help of derived class object to show inheritance
concepts.
2. Create a class publication which has title of book and writers name. Create
7 another class sale which accounts no. of sales for every month (upto 3 months)
and then calculate total sales.
3. Write a program to solve the ambiguity problem in inheritance where two
different classes are inherited from single base class and a new class is derived
from these two derived classes. How this problem is solved with the help of
virtual base class concept.
1. Create a base class that contains a function display(), displaying “I am in
base”
Function with same name display() is in derived class ,displaying “I am in
derive”.

STUDENT EVALUTION USING INHERITANCE

Aim
To write a C++ program for students evaluation concept using inheritance.

Algorithm

Step 1: create a class studentpersonal declare roll no,age,name,sex in protected

mode.

Step 2: Using a parameterized constructor initialize the values for all the data

members.

Step 3: Create another class studentmark that is inherited from the base class and
having the data members mark1,mark2,mark3,mark4 Using a parameterized
8 constructor initialize the value for mark1,mark2,mark3,mark4.

Step 4: Create another class called studentsports and declare score as protected
mode.

Step 5: Create a class studentresult and public inherited form studentmarks and
studentsports having the data members total,avg,grade..

Step 6: Calculate the total and avg and display the result.
Constructor Overloading and Copy Constructor
1. Write a program that accepts two values either integer or double. Design
functions that understand the input, add them and provide the correct output.
2. Create a base class called shape. Use this class to store two double type values
that could be used to compute the area of figures. Derive two specific classes
called triangle and rectangle from the base shape. Add to base class, a member
function
get_data() to initialize base class data members and another
member functions display_area() to compute and display the area of
figures. Mark the display_area() as avirtual function and redefine this
function in the derived class to suit their requirments.(Use pure virtual
function)
5. Write a program in C++ to demonstrate default constructor. Create a class
having two data members in the private section. Define a default constructor to
initialize these data members to initial value and display these values with the
9 help of member function
6. Write a program in C++ to demonstrate parameterized/constructor
overloading constructor. Create a class calculator that contains four data
members in it. Initialize data members with different values using
parameterized constructor and perform various arithmetic operation over these
values and display result on to
the computer screen.
7. Create a class called Triangle that stores the length of the base and height of a
right triangle in two private instance variables. Include a constructor that sets
these values. Define two functions. The first is hypot( ), which returns
the length of the hypotenuse. The second is area( ), which returns the
area of the triangle.

Mid term
Operator Overloading and Type conversion
Defining operator overloading, rules for overloading operators, overloading of
unary operators and various binary operators, overloading of new and delete
operators, type conversion - basic type to class type, class type to basic type,
class type to another class type.
List of programs:

1. Declare a class Number that contains two data member value1 and value2
of the type of integer, define constructor to give initial value, and perform
addition , subtraction, multiplication and division of these two numbers
using operating overloading of +,-,*,/ operator respectively [hint- binary
operator overloading using member function]
2. Declare a class Number1 that contains two data member value1 and
value2 of the type of integer, define constructor to give initial value, and
perform addition, subtraction, multiplication and division of these two
numbers using operating overloading of +,-,*,/ operator respectively
[hint- binary operator overloading using friend function]
3. Declare a class Number3 that contains a data member value of the type of
integer, define constructor to give initial value, and perform unary minus
,increment and decrement this number using operating overloading of -
,++,-- operator respectively [hint- Unary operator overloading
using member function]
4. Declare a class Number3 that contains a data member value of the type of
integer, define constructor to give initial value, and perform unary minus,
increment and decrement this number using operating overloading of -,+
+,-
- operator respectively [hint- Unary operator overloading using friend
function]
5. Define a class complex that contains two data member to store real and
imaginary part of the complex number. Create a function to get values
10 from the keyboard into these complex numbers, overload binary + and – to
calculate addition and subtraction of two complex numbers respectively
using member function .
6. Write a program to demonstrate explicit type conversion from basic type
to
user defined data type.
7. Write a program to demonstrate explicit type conversion from User
Defined data type to Basic data type data type.
8. Write a program to demonstrate explicit type conversion from one
user defined data type to another user defined data type.
9. Write a program in C++ to calculate mean value of n numbers using friend
function.
10. Write a program in C++ to find greater between two numbers using
friend function.
11. Write a program in C++ to display student’s information using friend
function.
12 . Write a program in C++ to swap between two numbers using friend function.
MATRIX OPERATION USING OPERATOR OVERLOADING

Aim
To create a C++ program for performing matrix operations using operator
overloading.

Algorithm

Step 1: Create 3 object for the matrix class m1, m2, m3.
Step 2: Read the elements of the matrix A and
matrix B using the getdata function in the
matrix class, which is invoked by the object
m1 and m2.
Step 3: Invoked the operator function for matrix addition using the
statement
m3=m1.operator+(m2). Step 4:
Create an object c of the type
matrix.
Step 5: Calculate matrix addition of the matrices of m1 and m2 and Store it
in the object c return c.
Step 6: Display the resultant matrix using the display member
Function of matrix by invoking through the object m3.
Step 7: Call the operator function for matrix subtraction using the
11
Statement m3=m1-m2.
Step 8: Pass the object m1 and m2 to the
operator function. Step 9: Create an object of
type matrix.
Step 10: Calculate matrix subtraction using the
object m1 and m2 and store in the object
created return c.
Step 11: Invoke the display function using the object m3.
Step 12: Call the operator function for matrix
multiplication using the statement
m3=m1*m2.
Step 13: Create an object of type matrix.
Step 14: Perform the matrix multiplication and store it
in the object and return it.
Step 15: Invoke the display function using the object m3.
POLYMORPHISM AND VIRTUAL FUNCTIONS

To gain experience with

 The concept of a polymorphic variable and its relationship to class


hierarchies
 the interaction between polymorphism and memory management
 how to distinguish between virtual and non-virtual overriding
 the concept of down casting
 multiple inheritance designing and using software frameworks

AREA CALCULATION USING VIRTUAL FUNCTIONS

Aim
To create a C++ program for calculating the area using the virtual
function.
12
Algorithm

Step 1: Create a class shape containing 2 members function and display


area , which is virtual.
Step 2: Create the object for the classes circle, rectangle and triangle.
Step 3: Overload operator << to display the results of addition and
subtraction operations on two polynomials.
Step 4: Display the results.

TO READ A VALUE OF DISTANCE FROM ONE OBJECT AND ADD WITH A VALUE
IN ANOTHER OBJECT USING FRIEND FUNCTION

Aim
To read a value of distance from one object and add with a value in
another object using friend function.

Algorithm

Step 1 : Create two classes AB and AC and store the value of distances.

Step 2: Declare friend function.


13
Step 3: Read the value from the classes.

Step 4: Perform addition to add one object of AB with another object of AC.

Step 5: Display the result of addition.

14 Virtual functions
Concept of binding - early binding and late binding, virtual functions, pure virtual
functions, virtual destructors & polymorphism.
1. WAP to use ‘this’ pointer to find elder from two person. Define a class
Person to store age of the person. Define constructor/member function
to give initial value to the data member age. And then define a function
elder to compare ages of two different person using this pointer to find
out the elder person.
2. WAP a program to show how member of the class are accessed through
the pointer to class using arrow operator (->).
3. Write a program to show the concept of virtual function with the help of
suitable programming example.
4. Create a simple “shape” hierarchy: a base class called Shape and derived
classes called Circle, Square, and Triangle. In the base class, make a
virtual function called draw( ),and override this in the derived classes.
Make an array of pointers to Shape objects that you create on the heap
(and thus perform upcasting of the pointers), and call draw( ) through
the base-class pointers, to verify the behavior of the virtual function. If
your debugger supports it, single-step through the code.
5. Modify Exercise 5 so draw( ) is a pure virtual function. Try creating an
object of typeShape. Try to call the pure virtual function inside the
constructor and see what happens. Leaving it as a pure virtual, give
draw( ) a definition.
6. Write a small program to show the difference between calling a virtual
function inside a normal member function and calling a virtual function
inside a constructor. The program should prove that the two calls produce
different results.

Simple File Handling


 Disk file I/O with streams
 File pointers
 Error handling in file I/O
 File I/O with member functions

1. Write a C program that manipulates a text file. The program must


implements the operation to modify a record, delete a record and
append new records.
2. Write C++ programs for the following operation to work like DOS Commands:
a. create a test1.txt file
b. copy test1.txt data into 2nd file
name test 2. txt c. compare test1.txt
15 and test2.txt
e. concatenate test1.txt and test2.txt into test3.txt and display the data
3. Write a C program to open two files containing integers (in sorted order) and
merge their contents.

Exception handling
1. Create a class with a main( ) that throws an object of class Exception
inside a try block. Give the constructor for Exception a String argument.
Catch the exception inside a catch clause and print the String argument.
Add a finally clause and print a message to prove you were there.

2. Create your own exception class using the extends keyword. Write a
16 constructor for this class that takes a String argument and stores it inside
the object with a String reference. Write a method that prints out the
stored String. Create a trycatch clause to exercise your new exception.

3. Write a class with a method that throws an exception of the type created
in Exercise 2. Try compiling it without an exception specification to see
what the compiler says. Add the appropriate exception specification. Try
out
your class and its exception inside a try-catch clause.

4. Define an object reference and initialize it to null. Try to call a method


through this reference. Now wrap the code in a try-catch clause to
catch the exception.

5. Create a class with two methods, f( ) and g( ). In g( ), throw an exception of


a new type that you define. In f( ), call g( ), catch its exception and, in the
catch clause, throw a different exception (of a second type that you
define). Test your code in main( ).

6. Repeat the previous exercise, but inside the catch clause, wrap g( )’s
exception in aRuntimeException.

Learning Outcomes:

On completion of this Course + Lab, the learner should be able to:


 Describe the syntax and semantics of the C++ programming language
 Explain the use of class and object.
 Work in a team to analyze engineering problems and develop C++
programs for solving these problems;
 Use the basic utilities and facilities for software development.
 debug and test software
 Develop a minor software in C++ language

Das könnte Ihnen auch gefallen