Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

C++ Lab Manual www.cranesvarsity.

com

ty
Lab Manual
Oop with C++
rsi
Va
es
an
Cr

© Cranes Varsity V2.1-2018-19 1/ 12


C++ Lab Manual www.cranesvarsity.com

SECTION -I
CHAPTER - 1
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

1. A lift can accept a maximum load of 1000kg. WAP to check how many people enter lift & what is the
total weight currently present in the lift? If it exceeds maximum limit, set a variable for overweight. If
overweight variable is set ring alarm.

2. Write a function Open_account () which will open an account for you in a bank. Decide what should
be the arguments for this functions & which arguments can take default values. Also decide which
arguments should be put in leading end & which one should be put in trailing end.

ty
3. A namespace ‘Garden; has a structure ‘Tree’. A namespace ‘Forest’ has a structure tree. Put
appropriate data’s within each structure. In main, create variables of each structure and access data

rsi
members within each structure.

CHAPTER - 2
PROCEDURAL APPROACH IN C++ Va
1. Write a C++ program that will display the calculator menu.

2. Write a C++ program (using function overloaded) to sort 10 integer values, or 10 float values, or 10
double values?

3. Write a function ‘Toggle_Bit’ to toggle a particular bit of a number. From main, invoke this function.
es
After that print the number in main. The changes should be reflected.

4. Raising a number to a power p is the same as multiplying n by itself p times. Write a function called
power that takes two arguments, a double value for n and an int value for p, and return the result as
an

double value. Use default argument of 2 for p, so that if this argument is omitted the number will be
squared. Write the main function that gets value from the user to test power function.

5. Write a function called zero_small() that has two integer arguments being passed by reference and
Cr

sets the smaller of the two numbers to 0. Write the main program to access the function.

CHAPTER - 3
OBJECT ORIENTED APPROACH IN C++

1. Define a class BOOK with the following specifications :


Private members of the class BOOK are
BOOK NO integer type
BOOKTITLE 20 characters

© Cranes Varsity V2.1-2018-19 2/ 12


C++ Lab Manual www.cranesvarsity.com

PRICE float (price per copy)


TOTAL_COST() A function to calculate the total cost for N number of copies where N is passed
to the function as argument.
Public members of the class BOOK are
INPUT() function to read BOOK_NO. BOOKTITLE, PRICE
PURCHASE() function to ask the user to input the number of copies to be purchased. It
invokes TOTAL_COST() and prints the total cost to be paid by the user.
Note : You are also required to give detailed function definitions.

2. Write the definition for a class called complex that has floating point data members for storing real
and imaginary parts. The class has the following member functions:
void set(float, float) to set the specified value in object
void disp() to display complex number object

ty
complex sum(complex) to sum two complex numbers & return complex number
1. Write the definitions for each of the above member functions.
2. Write main function to create three complex number objects. Set the value in two objects and call
sum() to calculate sum and assign it in third object. Display all complex numbers.

rsi
3. Create a class called Cylinder. This should provide member functions to :
a. Set the radius & height of the Cylinder
b. Calculate the volume of Cylinder ( pi*r*r*h)
c. Calculate the Area of the Cylinder ( 2 *pi *r *(r+ h) )
Va
In main, create object for the above class and show the operations.

4. Create a class Employee. Add appropriate data & functions inside class. Show the three applications
of “this” pointer
es
CHAPTER - 4
CLASS INTERNALS – CONSTRUCTORS AND DESTRUCTORS
1. Consider the definition of the following class:
an

class Sample
{
private:
int x;
double y;
public :
Cr

Sample(); //Constructor 1
Sample(int); //Constructor 2
Sample(int, int); //Constructor 3
Sample(int, double); //Constructor 4
};

i. Write the definition of the constructor 1 so that the private member variables are initialized to 0.

ii. Write the definition of the constructor 2 so that the private member variable x is initialized according to
the value of the parameter, and the private member variable y is initialized to 0.

© Cranes Varsity V2.1-2018-19 3/ 12


C++ Lab Manual www.cranesvarsity.com

iii. Write the definition of the constructors 3 and 4 so that the privatemember variables are initialized
according to the values of the parameters.

2. Dynamically create an array of 10 integers. Assign value of 0 to 9 in the allocated array. Print values of
the array. After that delete the array.

3. Write a class called Building. Add default constructor and parameterized constructor to it. Create two
objects in main. Make first object to invoke default constructor & second object to invoke
parameterized constructor.

4. In question 2, add a data member ‘location of building’ which is of the type char *. Dynamically
allocate memory for ‘location’ in constructors. Add destructors to de-allocate heap memory. Create two

ty
objects in main dynamically. Make first object to invoke default constructor & second object to invoke
parameterized constructor.

5. In question 3, create object which invokes copy constructor provided by the compiler. Analyze the

rsi
output. Then add user defined copy constructor which is doing deep copy & again analyze the output.

CHAPTER - 5

CLASS INTERNALS – STATIC AND CONSTANTS IN C++


Va
1. Create a class My_Account . Put suitable data members & member functions as static. Prove that
static data member comes into existence even before creating an object.
es
2. In class My_Account make appropriate data members & member functions as constant .Create a non
constant object , a constant object & try to find out which member functions can be invoked by these
objects .

a. WAP to count number of objects created for a class My_Account.


an

Hint: use static data member as count.

3. In a company, all the systems are connected in LAN. All the systems share a printer. When a system
is trying to print, it sets a variable ‘Printer _status’. When printing is over , Printer_status is reset. All
the systems have unique name which cannot be modified. Having this information, decide what data
Cr

members & member functions can be added. Also decide, out of these, which members can be made
as static and constant.

4. WAP to create a class called Human. The class should have at least five data & function members, of
which two data members should be static. The functions should perform actions specific to activities in
day to day life of humans. Check if there are any places where one can make use of static member
functions.

© Cranes Varsity V2.1-2018-19 4/ 12


C++ Lab Manual www.cranesvarsity.com

CHAPTER - 6

OPERATORS OVERLOADING & FRIEND KEYWORD

1. Design a class called My_String which represent a C++ string (denoted by char*). Overload
following operators in the class :
1. operator + which concatenates two strings
(Usage example: My_String + My_string, or My_String + char*)
2. Operator == to compare strings
3. Operator = to assign one string to another

ty
4. Operator! = to compare two strings

2. Create a class Shopping_list which has data member price [10] which stores prices of 10 items
purchased by a person.

rsi
1) Overload Operator [] to store the price of item specified by position.
2) Assume that the calculator has some problem while entering the data for price It takes one
rupee less than actual cost. Now overload ++ operator to increment the price of each item as
well as to get the total price for the whole shopping.
Va
3. Create a class called Distance which has data members as foot & inch. Write constructors to initialize
the data members. Overload ( ) operator to assign values to foot & inch. Add member functions to
return distance in inch. Create objects in main and invoke all the member functions.

CHAPTER - 7
GENERIC PROGRAMMING
es
1. Create a vector and read a list of names of fruits from cin into it. Sort the list and print it.

2. Create an object of class STL Vector. Insert 10 elements into vector. Assign values to 10 elements,
an

Then add 10 more elements to vector. Then Insert 10 times value 100 from positon 2, Finally, Erase
the values inserted. After each and every operation mentioned, print the contents of vector.

3. Produce a vector of squares of numbers 1 through 100. Print a table of squares. Take the square root
of the elements of that vector and print the resulting vector
Cr

4. Create a list and Sort it using only standard library algorithms.

5. Create a map named Employee with their name and id. Make use of array assignment to add the new
employees. Display the map. Write a code to remove the particular employee from the map and then
display it again.

CHAPTER – 8
GENERALIZATION - INHERITANCE

© Cranes Varsity V2.1-2018-19 5/ 12


C++ Lab Manual www.cranesvarsity.com

1. Implement a single inheritance program where base class is Clock & derived class is Alarm_Clock .
Base class should have at least one member with private access, one member with protected access,
one with public access. Clearly differentiate between different access specifiers. While designing
classes make sure you add valid data members & member functions. Also try to analyze the difference
between private, public & protected mode of inheritance.

2. In Q1, add default as well as parameterized constructors in both base & derived class. Create object
of Alarm_Clock class. Make it to call parameterized constructors of base as well as derived. Add
destructors also. Check the order of destructors when derived class object goes out of scope.

3. WAP to create a class called Math. Derive Geometry, Arithmetic & Algebra from it. Add
functionalities specific to these branches of Math to each of these derived classes. Perform
operations and reflect the last updated values in all the classes.

ty
4. WAP to implement multilevel inheritance

rsi
Mammal

Person
Va
Educated_Person
es
Add default constructor, parameterized constructor as well as destructor in all the classes. Create
object of Educated_Person class check the order of constructors & destructors.

5. WAP to implement hybrid inheritance. Remove the death of diamond problem.


an

Animal
Cr

Water_Animal
Land_Animal

Amphibian

© Cranes Varsity V2.1-2018-19 6/ 12


C++ Lab Manual www.cranesvarsity.com

CHAPTER - 9
RUN TIME POLYMORPHISM

Shape

Rectangle Circle

ty
1. For the above mentioned hierarchy, implement Hierarchical inheritance. Shape class has one
member function ‘draw’, override this function in Rectangle class and Circle class. Create object of
Rectangle class and Circle class and invoke function draw. Analyze which function is invoked.

rsi
2. For the class implemented in Q1, now create Shape class pointer
a) Make it to hold address of Shape class object, invoke draw function.
b) Make it to hold address of Circle class object, invoke draw function.
c) Make it to hold address of Rectangle class object, invoke draw function.
Va
Analyze which functions are being called.
Now make ‘draw’ function virtual in Shape class. Follow steps a ,b, c . Analyze
which functions are being called. Find out reason for different outputs.

3. For the above class diagram , show the need for virtual destructor.
es
4. Given the class
class AbstractString
{
an

public:
virtual void display ( ) = 0;
..........
};
Derive the classes : UpperString and LowerString .
Cr

Implement display( ) function to print the characters in the appropriate upper or lower cases.
Have a menu driven program allowing the user to enter the choice and display the characters in the
appropriate case .Use appropriate data members in the base class which would be required in the
derived classes. Implement dynamic binding.

SECTION -II
Specify the output of the following codes:

Question 1:
#include<iostream>
using namespace std;

© Cranes Varsity V2.1-2018-19 7/ 12


C++ Lab Manual www.cranesvarsity.com

class Base
{
protected:
int x;
public:
Base (int i){ x = i;}
};

class Derived : public Base


{
public:
Derived (int i):x(i) { }
void print() { cout << x ; }
};

ty
int main()
{
Derived d(10);
d.print();

rsi
}

Question 2:
#include <iostream>
using namespace std;
class Base {
Va
virtual void method() {std::cout << "from Base" << std::endl;}
public:
es
virtual ~Base() {method();}
void baseMethod() {method();}
};
an

class A : public Base {


void method() {std::cout << "from A" << std::endl;}
public:
~A() {method();}
Cr

};
int main(void) {
Base* base = new A;
base->baseMethod();
delete base;
return 0;
}
Question 3:

© Cranes Varsity V2.1-2018-19 8/ 12


C++ Lab Manual www.cranesvarsity.com

#include <iostream>
using namespace std;
class A
{
public:
void print() { cout << "A::print()"; }
};

class B : private A
{
public:
void print() { cout << "B::print()"; }
};

class C : public B

ty
{
public:
void print() { A::print(); }
};

rsi
int main()
{
C b;
b.print();
}
Va
Question 4:
#include <iostream>
using namespace std;
class A
es
{
int id;
public:
A (int i) { id = i; }
void print () { cout << id << endl; }
an

};

int main()
{
A a[2];
a[0].print();
Cr

a[1].print();
return 0;
}

Question 5:

#include<iostream>
using namespace std;
class Test {
int value;
public:
Test (int v = 0) {value = v;}

© Cranes Varsity V2.1-2018-19 9/ 12


C++ Lab Manual www.cranesvarsity.com

int getValue() { return value; }


};

int main() {
const Test t;
cout << t.getValue();
return 0;
}

Question 6:

#include<iostream>
using namespace std;

class Test1

ty
{
int x;
public:
void show() { }

rsi
};

class Test2
{
int x;
public:

};
virtual void show() { }
Va
int main(void)
{
cout<<sizeof(Test1)<<endl;
es
cout<<sizeof(Test2)<<endl;
return 0;
}

Question 7:
an

#include<iostream>
using namespace std;
class A
{
public:
virtual void fun() {cout << "A" << endl ;}
Cr

};
class B: public A
{
public:
virtual void fun() {cout << "B" << endl;}
};
class C: public B
{
public:
virtual void fun() {cout << "C" << endl;}
};

© Cranes Varsity V2.1-2018-19 10/ 12


C++ Lab Manual www.cranesvarsity.com

int main()
{
A *a = new C;
A *b = new B;
a->fun();
b->fun();
return 0;
}

Question 8:
#include<iostream>
using namespace std;

/* local variable is same as a member's name */


class Test

ty
{
private:
int x;
public:

rsi
void setX (int x) { Test::x = x; }
void print() { cout << "x = " << x << endl; }
};

int main()
{
Test obj;
int x = 40;
Va
obj.setX(x);
obj.print();
return 0;
}
es
Question 9:
#include <iostream>
using namespace std;
an

int fun(int a, int b = 1, int c =2)


{
return (a + b + c);
}

int main()
Cr

{
cout << fun(12, ,2);
return 0;
}

Question 10:

#include <iostream>
using namespace std;

class A
{

© Cranes Varsity V2.1-2018-19 11/ 12


C++ Lab Manual www.cranesvarsity.com

int id;
static int count;
public:
A()
{
count++;
id = count;
cout << "constructor called " << id << endl;
}
~A()
{
cout << "destructor called " << id << endl;
}
};

ty
int A::count = 0;

int main()
{

rsi
A a[2];
return 0;
}
Va
es
an
Cr

© Cranes Varsity V2.1-2018-19 12/ 12

You might also like