0% found this document useful (0 votes)
104 views12 pages

Practical List: Charotar University of Science &technology

1. The document lists practical assignments for an Object Oriented Programming with C++ course. It includes 14 programming assignments on topics like input/output using cout and cin, arrays, pointers, classes, structures, and more. 2. Students are asked to write programs that output text with formatting, create tables using manipulators, perform arithmetic on floating point numbers, find errors in code snippets, use scope resolution operators, use dynamic memory allocation, and illustrate concepts like function overloading and default arguments. 3. Later assignments involve defining classes for concepts like weights, batsmen, digits, and time to practice object-oriented design and illustrate differences between classes and structures in C++. Functions are defined both inside and outside

Uploaded by

mananadad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views12 pages

Practical List: Charotar University of Science &technology

1. The document lists practical assignments for an Object Oriented Programming with C++ course. It includes 14 programming assignments on topics like input/output using cout and cin, arrays, pointers, classes, structures, and more. 2. Students are asked to write programs that output text with formatting, create tables using manipulators, perform arithmetic on floating point numbers, find errors in code snippets, use scope resolution operators, use dynamic memory allocation, and illustrate concepts like function overloading and default arguments. 3. Later assignments involve defining classes for concepts like weights, batsmen, digits, and time to practice object-oriented design and illustrate differences between classes and structures in C++. Functions are defined both inside and outside

Uploaded by

mananadad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CHAROTAR UNIVERSITY OF SCIENCE &TECHNOLOGY

CHANDUBHAI S. PATEL INSTITUTE OF TECHNOLOGY


DEVANG PATEL INSTITUTE OF ADVANCE TECHNOLOGY AND RESEARCH
U & P.U. Patel Department of Computer Engineering
F.Y. B.Tech (CE/IT/EC/CSE)

Subject Name: Object Oriented Programming with C++


Semester: II
Subject Code: CE142
Academic year: 2017-18

Practical List
No. Aim of the Practical
Write a C++ program that will output this passage by Deepak Chopra. Make sure your output looks
exactly as shown here (including spacing, line breaks, punctuation, and the title and author). Use
cout and cin objects and endl manipulator.

“You alone are the judge of your worth


1.
and your goal is to discover infinite
worth in yourself, no matter what
anyone else thinks.” by Deepak Chopra

To include a quotation mark in your output, try this: \"

Write a program to create the following table. Use endl and setw manipulator.
1 2 3 4
2. 2 4 6 8
3 6 9 12
4 8 12 16
Write a C++ program to add two floating numbers using pointer. The result should contain only
two digits after the decimal.
3.
Use fixed, scientific and setprecision () manipulators for controlling the precision of floating
point numbers.
Find error in the following code and give reasons for each error:
Can we declare an array of references? Can we assign NULL value to reference variable? Is
Reference variable a pointer variable? Can we declare a reference variable without initializing it?
#include<iostream>
using namespace std;
4. int main()
{
int a =10, b=12;
int & r;
int & c = NULL;
int & d[2] = {a,b};
cout<<"r = "<< r;

Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat


return 0;
}
Find output of the following code: Explain how scope Resolution operator is used to access global version
of a variable.
#include<iostream.h>
#include<conio.h>
int m=30;
int main()
{
int m=20;
{
int m=10;
5.
cout<<”we are in inner block”<<endl;
cout<<”value of m=”<<m<<”\n”;
cout<<”value of ::m=”<<::m<<”\n”;
}
cout<<”we are in outer block”<<endl;
cout<<”value of m=”<<m<<”\n”;
cout<<”value of ::m=”<<::m<<”\n”;
getch();
return 0;
}

Find Error in the following code of a program and give explanation why these errors exist.

Refer this link for more understanding:


http://www.thegeekstuff.com/2012/06/c-constant-pointers/
Note: There is mistake in Balagury 6th edition for syntax of constant pointer and pointer to
constant.
1. //This is an 2. //This is an 3. //This is an example
example of example of of constant pointer
constant pointer pointer to to a constant
#include <iostream> constant #include <iostream>
6. using namespace std; #include <iostream> using namespace std;
int main() using namespace int main()
{ std; {
int var1 = 35,var2 int main() int var1 = 0,var2 = 0;
= 20; { const int* const ptr =
int *const ptr = int var1 = 43; &var1;
&var1; const int* ptr = *ptr = 1;
ptr = &var2; &var1; ptr = &var2;
cout<<"var1= *ptr = 1; cout<<"Var1 =
"<<*ptr; var1=34; "<<*ptr;
return 0; cout<<"var1 = return 0;
} "<< *ptr; }
return 0;

Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat


}

Put comment infront


of *ptr = 1
Explain why value
of var1 changes to
34 inspite ptr being
pointer to constant
??
Write a program to enter a size of array. Create an array of size given by user using “new”
Dynamic memory management operator (free store operator). Enter the data to store in array
7. and display the data after adding 2 to each element in the array. Delete the array by using “delete”
memory management operator.

Find the output of following program. Explain the use of bool data type.

#include<iostream>
using namespace std;
int main()
{
bool a = 321, b;
cout << "Bool a Contains : " << a<<endl;
8. int c = true;
int d = false;
cout<<"c = "<<c <<endl<<"d = "<<d;
c = a + a;
cout << "\nInteger c contain : " << c;
b = c + a;
cout << "\nBool b contain : " <<b;
return 0;
}
Define three functions named convert (). First function takes rupees as an input argument and
converts into paisa, second function takes meter as an input argument and converts into centimeter
9. and last function takes character as an input argument and converts entered small character into
capital. Here, rupee is in integer and meter is in float.Use concept of Function Overloading.
Function overloading is also known as Compile Time Polymorphism or static binding.
Define four function void swap () which accepts two arguments by reference and swap the values.
10. First function swaps two characters, second function swaps two integers, third function swaps two
floats values and fourth function swaps two double values. Use the concept of call by reference in
all four functionsand function overloading and inline function.
Declare an array of 5 double elements and assign some values to it. Create a function setvalue( )
which takes the index of the array as an input and Returns by Reference double value and change
the value of the array element stored in that index.
11. For eg: double arr[] = {23.71, 45.6, 88.1, 19.1, 54.7}.

A function call such as setvalue (2) = 70.23 will result in the following array
double arr[] = {23.71, 45.6, 70.23, 19.1, 54.7} . (Hint: Declare array as a global variable).

Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat


Volume of Ellipsoid = (4/3) × pi × radius1 × radius2 × radius3. Write a program having function
volume () which takes three float arguments: radius1, radius 2 and radius3 and returns the volume
of an Ellipsoid. Use default argument of 2 for radius1, 3 for radius2 and 4 for radius3 so that if
12.
arguments are omitted then the volume of Ellipsoid is always 100.48. Write a main ( ) function that
gets values from the user to test this function.

Create a C++ Structure Weighthaving private data members: float kg, grams. A member function
13. getvalue () should enter their values. Another member function putvalue () should display their
values.
Create a C++ ClassWeight having private data members: float kg, grams. A member function
14. getvalue () should enter their values. Another member function putvalue () should display their
values. Define both functions inside the class. Member function defined inside the class
behaves like an inline function. Illustrate the difference between C++ Structure and C++ Class.
Write a C++ program having class Batsman. It has private data members: batsman name, bcode (4
Digit Code Number), innings, not out, runs, batting average. Innings, not out and runs are in integer
and batting average is in float.
Define following function outside the class using scope resolution operator.
15. 1) Public member function getdata()to read values of data members.
2) Public member function putdata()to display values of data members.
3) Private member function calcavg() which calculates the batting average of a batsman.
Also make this outside function inline.
Hint : batting average = runs/(innings - notout)
Define class Digit having int ‘n’ as data member. Define member function enter() to enter the data
and show() to print the data. A class has member function compare() that displays whether the first
object is smaller, greater or same as compared to second object. (Function compare() should
16.
support: int x = d1.compare(d2); where d1 and d2 are objects of class Digit).

Use Concept of Object as Function Arguments.

Create a class time having data members: hr, min and sec. A member function should enter their
values. Another member function print the time in the format 11:59:59. One member function
should add two objects of type time passed as arguments such that it supports t3.add(t1,t2). The
17. second member function should add two objects of type time passed as arguments such that it
supports t3=t1.add(t2). Write a main( )program to test all the functions.
Use concepts of Object as Function Arguments and function returning object.

Define a classDist with int feet and float inches. Define member function that displays distance in
1’-2.5” format. Also define member function scale ( ) function that takesobject by reference and
18. scale factor in float as an input argument. The function will scale the distance accordingly.
For example, 20’-5.5” and Scale Factor is 0.5 then answer is 10’-2.75”

Define class Mobile having data members: year of manufacture, price. A class has member
19. functions to get and print data. Define member function validity ( ) that checks whether the mobile
is older than 2005 or not. If mobile is older, it is invalid. Input data for 4 mobiles in main ( ) and
display information about the phone which is invalid. Use the concept of Array of Objects.
Create a Class Gate for students appearing in Gate (Graduate Aptitude test for Engineering) exam.
There are three examination center Vadodara, Surat, and Ahmedabad where Gate exams are
20. conducted. A class has data members: Registration number, Name of student, Examination center.
Class also Contains static data member ECV_Cnt, ECS_Cnt and ECA_Cnt which counts the
number of students in Vadodara, Surat and Ahmedabad exam center respectively. Class Contains
two Member function getdata () which gets all information of students and counts total students in
Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat
each exam center and pudata () which prints all information about the students. Class also contains
one static member function getcount () which displays the total number of students in each
examination center. Write a program for 5 students and display the total number of students in each
examination center. Use static data member, static member function and Array of Objects.
Define a class house with length and width. Define another class office with length and width. Both
classes have member functions to input and print data. Write a function that receives objects of
21. both classes and compare them according to their area. Also define main( ) to test the function.
Define all functions outside the class.
Use the concept of friend function.
Create a Class Date having data members: int dd, mm, yyyy. Class has one member function to
input the dates and another member function which prints the dates. Write a main() function which
22. takes two dates as input. Write a friend function swapdates() which takes two objects by reference
of type Date and swaps both the dates.Use the concept of Friend function which takes objects by
reference
Create a class Customer having data members: name of the customer and customer number in
integer and member function to get customer data. Create another class Manager having data
members: name of manager and employee id in integer and member function to get managers data.
23. Class Manager also have member function get_cust_data () which takes objects of class Customer
as input and prints the customers details and is a friend function of class Customer . Write a main ()
function to test all this function. Use the concepts of Member function of one class can be a
Friend Function of another class

Create a class Child having data members: name of the child and gender and a member function to
get and print child data. Create another class Parent which is a friend class of child class. Class
24. Parent have member function ReadChildData() which takes child’s object by reference as input
argument and Reads the childs data and DisplayChildData() which takes childs object as argument
and displays childs data. Use the concepts of Friend Class.
Check the following C++ code and find if there is any error in code, give justification for the error,
correct the code and write the output:

1. // This is an example of const member functions

#include<iostream>
using namespace std;
class sample
25.
{
int m, n;
public:
void getdata();
void putdata() const;
};
void sample::getdata()
{
cout<< "Enter m & n";
cin>>m>>n;
}
void sample::putdata() const
Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat
{
m=12;
n=34;
cout<< " m = "<<m<<"n= "<<n;
}
int main()
{
sample s1;
s1.getdata();
s1.putdata();
return 0;
}
2. // This is an example of (a)Pointer to data members, (b)Pointer to member functions

(a) #include<iostream>
using namespace std;
class student
{
public:
int roll_no;
};
int main()
{
// declaring pointer to data member
int student :: *p1 = &student::roll_no;
student s;
student *optr = &s;
s->*p1 = 42;
cout<<"Roll no is "<<s->*p1<<endl;
optr.*p1 = 45;
cout<<"Roll no is"<<optr.*p1<<endl;
return 0;
}

(b) #include<iostream>
class employee
{
public:
void hello()
{
cout<<"Hi hello"<<endl;
}

Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat


};
int main()
{
// declaring pointer to member function hello
void (employee ::*fp)() = &employee::hello;
employee e;
employee *optr = &e;
(e->*fp)();
(optr.*fp)();
return 0;
}

3. // This is an example of Local Classes

#include<iostream>
using namespace std;
void testlocalclass()
{
class Test
{
static int cnt;
public:
void set()
{cout<<"Enter Count: "; cin>>cnt; }
void get();
};
void Test:: get()
{ cout<<"Count: = " <<cnt; }
Test t;
t.set();
t.get();
}
int main()
{
testlocalclass();
return 0;
}
Write a C++ program having class time withdata members: hr, min and sec. Define following
member functions.
26. 1) getdata() to enter hour, minute and second values
2) putdata() to print the time in the format 11:59:59
3) default constructor
4) parameterized constructor
Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat
5) copy constructor
6) Destructor.
Use 52 as default value for sec in parameterized constructor.
Use the concepts of default constructor, parameterized constructor, Copy constructor,
constructor with default arguments and destructor.
Create a class Number having int num as member. The class has input and output functions.
Overload unary operator (++) such that it supports N1=N2++ and N3=++N1 and Overload unary (-
) such that it supports
27. N3 = - N3. Also define default, parameterized and copy constructor for the class. Also explain use
of nameless object in operator overloading.Use the concept of Overloading Unary
Operators.Operator overloading is also known as Compile Time Polymorphism or static
binding.

Create a class complex having data members int real , img and member function to print data.
28. Overload Unary operator (-) using friend function such that it supports –C1 where C1 is the object
of class complex. Also define default, parameterized and copy constructor for the class.
Use the concept of Overloading Unary Operators with friend function.
Define a class NUM having two integer members a and b. A class has member functions for input
and output. Overload the operators – and = = such that it supports the following statements:
29. NUM N1, N2, N3; N3=N1-N2; if(N1==N2){…}

Use the concept of Overloading Binary Operators.


Create a class Measure having members: meter and cm. The class has get( ) and put( ) functions.
30. Overload operator + and – such that they support M1=M2+15 and M3=M1 – 4.5. Also overload +
and – such that they support M1=5.0+M2 and M3=2.0 – M4. Write a main( ) to test the class.
Use the concept of Overloading Binary Operators with friend function.
Create a class Money having integer rupee, paisa as data. Define appropriate functions to enter and
display data. Also define function that supports the statement: obj1=115. Here obj1 is object of
31. class and 115 is integer that represents paisa. After execution of the statement obj1 will be 1 rupee
15 paisa.
Use the concept of Type conversion from basic type to class type.

Create a class Celsius with float. Define appropriate member functions such that it support the
32. statements: float temperature; temperature=C2;
Use the concept of Type conversion from class type to basic type.
Define a class KG having data member float kg and class POUND having data member float lb.
Both the classes have default constructor and member functions to get and show data. (1 kg =
2.20462 lb). Define appropriate member functions such that they support the statements in main( ):
KG K;

33. POUND P;
P=K;
Use the concepts of Type conversion from class type to class type.
Write this Program in two ways.
Define appropriate member function in class KG.
Define appropriate member function in class POUND
Define a Base Class Vegetable having data member Color and member function getdata() which
34. takes color as an input and putdata() which print the color as an output.
Vegetable Class has one subclass named Tomato having data members weight and size and
member function gtdata() which takes weight and size as an input and ptdata()
Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat
which prints weight and size as output. Write a C++ Program which inherits the data of Vegetable
class in Tomato class using Single Inheritance.
Write a program to create a class Medicine which stores type of medicine, name of company, date
of manufacturing. Class Tablet is inherited from Medicine. Tablet class has name of tablet,
quantity per pack, price of one tablet as members. Class Syrup is also inherited from Medicine and
35. it has quantity per bottle, dosage unit as members. Both the classes contain necessary member
functions for input and output data. Write a main( ) that enter data for tablet and syrup, also display
the data.Use the concepts of Hierarchical Inheritance.

Create a class shape having data member shape_name and member function to get and print
shape_name. Derive a Class Circle which is inherited publicly from class shape and having data
members radius of a circle and member function to get and print radius of a circle. Derive a Class
36. Area which is inherited publicly from Class Circle and having data members area_of_circle and
member function display () which displays area of a circle. Use object of class Area in main ()
function and get and display all the information.Use the concepts ofMultilevel Inheritance.

Define a class Hospital having rollno and name as data members and member function to get and
print data. Derive a class Ward from class Hospital having data members: ward number and
member function to get and print data. Derive another class Room from Hospital having data
37. member bed number and nature of illness and member function to get and print data. Derive class
Patient from Class Ward and Class Room. In main () declare 5 object of Class Patient and get and
display all the information.
Use the concept of Virtual Base Class and Hybrid Inheritance.
Create a Class alpha having data member: int x and one argument constructor which initializes the
value of x. It also has member function which displays the value of x. Create another class beta
which contains data member: float y and one argument constructor which initializes the value of y.
It also has member function which displays the value of y. Create a Class Gamma which publicly
38. inherits from class alpha and class beta and has two data members: int m, n and a constructor which
passes argument to the base class constructor as well as initializes its own data members. Class
Gamma also has member function to print the values of m and n. Write main function which
creates object of class Gamma which passes values of base class constructor as well as derived
class constructor. Use the concept of Multiple Inheritance and Constructor in Derived Class.
What is the output of the following code:
(a) Pointer to Objects
#include<iostream>
using namespace std;
class product
{
int code;
float price;
public:
39. void getdata(int a, float b)
{
code=a;
price=b;
}
void show()
{
cout<<"Code: "<<code<<endl;
cout<<"Price: "<<price<<endl;
}
};
Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat
int main()
{
product * p = new product;
product *d = p;

int x,i;
float y;
cout<<"Input code and price for product: ";
cin>>x>>y;

p->getdata(x,y);

d->show();
}
(b) this pointer

#include<iostream>
using namespace std;
class student
{
int roll_no;
float age;
public:
student(int r, float a)
{
roll_no = r;
age = a;
}
student & greater (student & x)
{
if(x.age>=age)
return x;
else
return *this;
}
void display()
{
cout<<"Roll No "<<roll_no<<endl;
cout<<"Age "<<age<<endl;
}
};
int main()
{
student s1 (23,18),s2 (30,20),s3 (45,16);
student s = s1.greater(s3);
cout<<"Elder Person is :"<<endl;
s.display();

(c) Pointers to Derived Objects

#include<iostream>

Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat


using namespace std;
class BC
{
public:
int b;
void show()
{
cout<<"b = "<<b<<endl;
}
};

class DC : public BC
{
public:
int d;
void show()
{
cout<<"b = "<<b<<endl;
cout<<"d = "<<d<<endl;
}
};

int main()
{
BC *bptr;
BC base;

bptr = &base;
bptr->b = 100;
cout<<"bptr poins to base objects"<<endl;
bptr->show();
DC derived;
bptr = &derived;
bptr->b = 200;

/*bptr->b = 300;*/ // wont work


cout<<"bptr now points to derived object"<<endl;
bptr->show();
DC *dptr;
dptr=&derived;
dptr->d=300;
cout<<"Dptr is derived type pointer"<<endl;
dptr->show();
return 0;
}

Create a class Media that stores the title (a string) and price (float). Class Media has two argument
constructor which initializes data members of class Media. Also declare a virtual function display ()
40. in Class Media. From the class Media derive two classes: Class book, which contains data member
page count (int): and Class tape, which contains data member playing time in minutes (float). Both
Class book and Class tape should have a constructor which initializes base class constructor as well

Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat


as its own data members and display ( ) function which displays book details and tape details
respectively. Write a main ( ) to test book and tape classes by creating instances of them, asking the
user to fill data and displaying them. Use the concept of Virtual function and Constructor in
Derived Class. Virtual function is also known as Runtime Polymorphism or Dynamic
Binding.

Create a Abstract class vehicle having average as data and pure virtual function getdata() and
putdata(). Derive class car and truck from class vehicle having data members: fuel type (petrol,
41. diesel, CNG) and no of wheels respectively. Write a main ( ) that enters the data of two cars and a
truck and display the details of them. Use the concept ofAbstract Base class and Pure Virtual
functions.

Write a program that creates a text file that contains ABC…Z. A program should print the file in
reverse order on the screen. i.e. ZYX…BA. Use concept ofOpening the file using constructor
42. and open () function. Use all error handling functions like eof() , fail() , bad() , good() and
functions for manipulation of file pointer like seekg() and tellg().

Prepared by: Khushboo Patel , Kruti Dhyani , Mayuri Popat

You might also like