LABMANUALC++Using OOP
LABMANUALC++Using OOP
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
Step 3: call the init member function of the class bank account through the object
created.
Step 5: Display a menu with the following options create deposit withdraw
display.
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 11: Check whether this account number is already exiting in the list. If true
Step 13: Update the balance by adding the amount deposited to the balance.
Step 15: If the choice is withdraw, call the withdraw member function.
then.
Step 24: If the choice is display, invoke the display member function using the
Object.
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 2: Declare the pointer variable for author, title, and publisher and the
Variable for price and stock position.
Step 6:If the choice is create, read the title, author, publishes, price
and stock
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 12:If the choice is display, then display all the details such as title, author,
price, publishes and stock position.
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”.
Aim
To write a C++ program for students evaluation concept using inheritance.
Algorithm
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
Aim
To create a C++ program for calculating the area using the virtual
function.
12
Algorithm
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 4: Perform addition to add one object of AB with another object of AC.
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.
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.
6. Repeat the previous exercise, but inside the catch clause, wrap g( )’s
exception in aRuntimeException.
Learning Outcomes: