Professional Documents
Culture Documents
Gharuan, Mohali
Institute/Department: University Institute of Engineering
Division: Academic Unit - 4
Subject Name: Object Oriented Programming Using C++
Subject Code: CST-152
Assignment No.: 3
Max. Marks: 12
Course Outcomes:
CO Title Level
Number
It will provide the environment that allows students to Understand
CO1
understand object-oriented programming Concepts.
Students will demonstrate basic experimental skills for Remember
differentiating between object-oriented and procedural
CO2
programming paradigms and the advantages of object-oriented
programs.
Ability to demonstrate their coding skill on complex Understand
CO3 programming concepts and use it for generating solutions for
engineering and mathematical problems.
Students will develop skills to understand the application of Understand
classes, objects, constructors, destructors, inheritance, operator
CO4
overloading and polymorphism, pointers, virtual functions,
templates, exception handling, file operations and handling.
Questions:
int main()
{
char character;
int integer;
ofstreamout_stream;
ifstreamin_stream;
/* Create a file containing two integers */
out_stream.open("Integers");
out_stream<< 123 << ' ' << 456;
out_stream.close();
/* Attempt to read a character, then an integer,
then a character again, then an integer again,
from the file "Integers" just created. */
in_stream.open("Integers");
in_stream>> character >> integer;
cout<< "character: '" << character << "'\n";
cout<< "integer: " << integer << "\n";
in_stream>> character >> integer;
cout<< "character: '" << character << "'\n";
cout<< "integer: " << integer << "\n";
in_stream.close();
return 0;
}
CO3
Q2. Write a program which outputs its own C++ source file to the
screen.
Q3. Write a program that (i) starts with the output test statement: CO1
cout<< "Testing: " << 16/2 << " = " << 4*2 <<".\n\n"; CO2
and then (ii) outputs its own C++ source file to a file called
"WithoutComments.cpp" and to the screen, but omitting any of
the comments enclosed in "/* ... */" markers (and omitting the
markers themselves). The new program in the file
"WithoutComments.cpp" should compile and run in exactly the
same way as the program from which it was generated.
Q4. Write a program to produce the output as shown below: CO1
Results:
x value y value expressions results
10 |5 | x+=y | x=15
10 |5 | x-=y-2 | x=7
10 |5 | x*=y*5 | x=250
10 |5 | x/=x/y | x=5
10 |5 | x%=y | x=0
Q5. Write C++ program to display a table that represents a Pascal CO4
triangle of any size. In Pascal triangle, the first and the second
rows are set to 1. Each element of the triangle (from the third
row downward) is the sum of the element directly above it and
the element to the left of the element directly above it. See the
example Pascal triangle(size=5) below:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
2 Grou Q1. Write a program which counts and displays the number of CO3
pB characters (including blanks) in its own source code file.
Q2. Assuming that a text file named FIRST.TXT contains some text CO4
written into it, write a function named vowelwords(), that reads
the file FIRST.TXT and creates a new file named
SECOND.TXT, to contain only those words from the file
FIRST.TXT which start with a lowercase vowel (i.e., with 'a', 'e',
'i', 'o', 'u').
For example, if the file FIRST.TXT contains Carry umbrella and
overcoat when it rains Then the file SECOND.TXT shall contain
umbrella and overcoat it
Q3. What is the output of this program? CO2
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int length;
char * buffer;
ifstream is;
is.open ("sample.txt", ios :: binary );
is.seekg (0, ios :: end);
length = is.tellg();
is.seekg (0, ios :: beg);
buffer = new char [length];
is.read (buffer, length);
is.close();
cout.write (buffer, length);
delete[] buffer;
return 0;
}
Q4. Write a C++ program to add two fractions and display the result CO4
fraction. Your program will prompt the user to input fraction 1
and fraction 2. The numerator and denominator of each fraction
are input separately by space. See the example output below.
You will need to use a C++ structure to define a fraction. The
structure has two members: numerator and denominator.
Enter fraction 1(numerator denominator): 1 2
Enter fraction 2(numerator denominator): 2 5
Result: 9/10
Q5. What is a virtual pointer or vfptr and virtual function table CO1
vftable? explain with diagram and example?
3 Grou Q1. Without using an array write a program that prints itself out CO2
pC backwards on the screen.
Q2. If we have object from ofstream class, then default mode of CO4
opening the file is _____
a. ios::in
b. ios::out
c. ios::in|ios::trunc
d. ios::out|ios::trunk
Q3. Write a function in C++ to count and display the number of CO4
lines not starting with alphabet 'A' present in a text file
"STORY.TXT".
Example:
If the file "STORY.TXT" contains the following lines,
The rose is red.
A girl is playing there.
There is a playground.
An aeroplane is in the sky.
Numbers are not allowed in the password.
The function should display the output as 3
Q4 Any character is entered by the user; write a program to CO2
determine whether the character entered is a capital letter, a
small case letter, a digit or a special symbol. The following table
shows the range of ASCII values for various characters.
Characters ASCII Values
A–Z 65 – 90
a–z 97 – 122
0–9 48 – 57
5 Grou Q1. What will be the changes made to the content of the file after CO3
pE running the code?why?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
//create a text file named file before running.
ofstreamofile;
ofile.open ("file.txt");
ofile<< "geeksforgeeks", 13;
ofile.seekp (8);
ofile<< " geeks", 6;
ofile.close();
return 0;
}
Q2. Write a program in C++ to read the content from a text file CO3
OUT.TXT, count and display the number of alphabets present
in it.
Q3. Which one is always faster in writing on C++? CO1
a) Writing to a file
b) Writing to memory
c) Reading from the network
d) None of the mentioned
Q4. To convert temperatures written in Fahrenheit to Celsius CO2
(Centigrade), you subtract 32, multiply by 5 and then divide by
9. To convert Celsius to Absolute Value (Kelvin), you add
273.15. Write a program that displays a temperature conversion
chart on the screen as follows:
Fahrenheit Celsius Absolute Value
0 -17.78 255.37
20 -6.67 266.48
40 4.44 277.59
... ...... ......
... ...... ......
300 148.89 422.
Q5. Write a C++ program that will output the multiplication table CO3
as show below:
0 1 1 1 1
-1 0 1 1 1
-1 -1 0 1 1
-1 -1 -1 0 1
-1 -1 -1 -1 0
Q5. Program to write and read an object in, from binary file using CO4
write() and read() in C++.
9 Grou Q1. Write a program which counts and displays the number of CO1
pI characters (including blanks) in its own source code file.
Q2. Assuming that a text file named FIRST.TXT contains some text CO3
written into it, write a function named vowelwords(), that reads
the file FIRST.TXT and creates a new file named
SECOND.TXT, to contain only those words from the file
FIRST.TXT which start with a lowercase vowel (i.e., with 'a', 'e',
'i', 'o', 'u'). For example, if the file FIRST.TXT contains Carry
umbrella and overcoat when it rains Then the file
SECOND.TXT shall contain umbrella and overcoat it.
Q3. write C++ program to display a table of numbers as shown CO4
below:
1 2 3 4 5
6 7 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Q4. Write a program that will ask the user to input three integer CO3
values from the keyboard. Then it will print the smallest and
largest of those numbers.
Q5.WAP Read Characters from One file and Write them in Toggle CO4
Case in Other using C++ file stream.
10 Grou Q1. Write a program that (i) starts with the output test statement: CO3
pJ cout<< "Testing: " << 16/2 << " = " << 4*2 <<".\n\n";
and then (ii) outputs its own C++ source file to a file called
"WithoutComments.cpp" and to the screen, but omitting any of
the comments enclosed in "/* ... */" markers (and omitting the
markers themselves). The new program in the file
"WithoutComments.cpp" should compile and run in exactly the
same way as the program from which it was generated
Q2. What is the Output of following program? CO3
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int main ()
{
ifstreamifile;
ifile.open ("text.txt");
char last;
ifile.ignore (256, ' ');
last = ifile.get();
cout<< "Your initial is " << last << '\n';
ifile.close();
return 0;
}
Q3. WAP Open a file and append data to the end of the file. CO4
Q4. The Fibonacci sequence a(1), a(2), a(3), ..., a(n), ... is defined CO3
by
• a(1) =1
• a(2) = 1
• a(n) = a(n-1) + a(n-2), for all n > 2
This generates the sequence
1, 1, 2, 3, 5, 8, 13, 21, ...
Write a C++ function "fibonacci(...)" that computes the Fibonacci
number corresponding to its positive integer argument, so that,
for example, fibonacci(7) == 13.
Q5. Write a C++ program that will display the calculator menu. CO4
The program will prompt the user to choose the operation choice
(from 1 to 5). Then it asks the user to input two integer vales for
the calculation. See the sample below.
MENU
1. Add
2. Subtract
3. Multiply
4. Divide
Enter your choice: 1
Enter your two numbers: 12 15
Result: 27
Continue? Y
The program also asks the user to decide whether he/she
wants to continue the operation. If he/she input ‘y’, the
program will prompt the user to choose the operation gain.
Otherwise, the program will terminate.
11 Grou Q1. What is the Output of following program? CO1
pK #include<iostream>
#include<fstream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
ifstreamifile;
ifile.open ("text.txt");
cout<< "Reading data from a file :-" <<endl ;
int c = ifile.peek();
if ( c == EOF ) return 1;
if ( isdigit(c) )
{
int n;
ifile>> n;
cout<< "Data in the file: " << n << '\n';
}
else
{
string str;
ifile>> str;
cout<< "Data in the file: " << str << '\n';
}
ifile.close();
return 0;
}
Q2.WAP to depict the working of seekg() and seekp(). CO4
Q3. Given positive two integers m and n such that m<n, the greatest CO3
common divisor of m and n is the same as the greatest common
divisor of m and (n-m). Use this fact to write a recursive
definition of the function "greatest_common_divisor(...)",
which takes two positive integer arguments and returns their
greatest common divisor. Test your function in a suitable main
program.
Q4. Write a function to count the number of blank present in a text CO3
file named "OUT.TXT".
Q5. Write a C++ program that will output the multiplication table as CO1
show below:
================================================
==
MENU
================================================
==
1. Add student records
2. Delete student records
3. Update student records
4. View all student records
5. Calculate an average of a selected student’s
scores
6. Calculate total scores of a selected student
7. Display the highest and lowest scores
8. Sort students’ records by ID
9. Sort students' records by total score
Enter your choice:1
Q5. Write a program which outputs its own C++ source file to the CO4
screen.