2nd Year Computer Science Exercise
2nd Year Computer Science Exercise
Q4. Write a C++ program implementing a class with the name Circle having two functions with
the names GetRadius and CalArea. The functions should get the value for the radius from the
user and then calculate the area of the circle and display the results.
#include <iostream>
class Circle {
private:
double radius;
public:
void GetRadius() {
void CalArea() {
cout << "The area of the circle is: " << area << endl;
};
int main() {
Circle c;
c.GetRadius();
c.CalArea();
return 0;
}
Q5. Write a C++ program implementing inheritance between Employee (base class) and
Manager (derived class).
#include <iostream>
class Employee {
protected:
string name;
int id;
public:
void GetDetails() {
void ShowDetails() {
};
string department;
public:
void GetManagerDetails() {
GetDetails();
void ShowManagerDetails() {
ShowDetails();
};
int main() {
Manager m;
m.GetManagerDetails();
m.ShowManagerDetails();
return 0;
These programs address the tasks described in the image you shared. The first program
calculates the area of a circle based on the user's input, while the second program
demonstrates inheritance by creating a Manager class that inherits from an Employee class.
We have Shape as a base class and Circle, Rectangle, Triangle, and Square as its derived classes.
*Answer:*
2. Draw four rectangles below it labeled Circle, Rectangle, Triangle, and Square (Derived
Classes).
3. Draw arrows pointing from each of the derived class rectangles up to the Shape rectangle,
indicating that these classes inherit from Shape.
Q.7 Write a C++ program implementing a class with the name ConstDest having constructor
and destructor functions in its body.
*Answer:*
Here is a simple C++ program to create a class named ConstDest with a constructor and
destructor:
#include <iostream>
class ConstDest {
public:
// Constructor
ConstDest() {
// Destructor
~ConstDest() {
};
int main() {
return 0;
Q.8 Write a C++ program implementing a class with the name Time. This class should have a
constructor to initialize the time (hours, minutes, seconds). The class should have another
function named ToSecond() to convert and display the time into seconds.
*Answer:*
Here is a simple C++ program to create a class named Time with a constructor and a function
ToSecond():
#include <iostream>
class Time {
private:
int hours;
int minutes;
int seconds;
public:
hours = h;
minutes = m;
seconds = s;
}
// Function to convert time to seconds
int ToSecond() {
void displayTimeInSeconds() {
cout << "Time in seconds: " << ToSecond() << " seconds" << endl;
};
int main() {
t.displayTimeInSeconds();
return 0;
CHAPTER 9
Answer:*
- File Definition*: A file is a container in a computer system for storing information. It can
contain text, images, audio, video, or other types of data. Files are identified by their names
and often have extensions that indicate their type, like .txt for text files or .jpg for image files.
- Types of Files*:
1. *Text Files*: These files contain plain text and can be opened and edited using text editors
like Notepad. Examples include .txt, .csv.
2. *Binary Files*: These files contain data in binary format (0s and 1s), which is not human-
readable. Examples include .exe (executable files), .bin.
3. *Image Files*: These files store visual data. Examples include .jpg, .png, .gif.
4. *Audio/Video Files*: Files that store sound and video information. Examples
include .mp3, .mp4, .avi.
5. *Executable Files*: Files that contain programs which can be executed. Examples
include .exe, .bat.
6. *Archive Files*: These files are used to compress and store multiple files. Examples
include .zip, .rar.
*Answer:*
2. *Reading a File*: Accessing and retrieving the content of a file for use.
4. *Appending to a File*: Adding data to the end of a file without removing existing data.
5. *Closing a File*: Terminating access to a file after read/write operations are done.
Answer:
- Output Streams*: These are used to write data to a destination (like a file, display screen, or
printer). Examples include cout in C++ for writing output to the console.
Q.7 What is meant by the term mode of file opening? Describe different modes of opening a
file.
Answer:
- Mode of File Opening*: This refers to how a file is accessed or used by a program. It
determines the type of operations that can be performed on the file.
1. Read Mode (r)*: Opens a file for reading. The file must exist.
2. Write Mode (w)*: Opens a file for writing. If the file exists, it will be overwritten; if not, a
new file will be created.
3. Append Mode (a)*: Opens a file for writing at the end of the file without deleting existing
content. If the file doesn’t exist, it creates a new file.
4. Read and Write Mode (r+)*: Opens a file for both reading and writing. The file must exist.
5. Write and Read Mode (w+)*: Opens a file for both writing and reading. It overwrites the
existing file or creates a new one.
6. Append and Read Mode (a+)*: Opens a file for reading and appending. The file is created if
it doesn’t exist.
Answer:
- File Handling Definition: File handling refers to the process of creating, opening, reading,
writing, and closing files in a program. It is essential for data storage and management in
various applications.
- Detail Explanation: In programming, file handling involves a set of operations that allow
programs to interact with files on a storage device. This interaction is crucial for saving data,
retrieving data for processing, and updating stored information. Through file handling,
programs can manage user data, configuration files, logs, and much more.
1. *Open*: A file is opened using a specific mode that determines what can be done with the
file (read, write, append).
2. *Process*: Operations like reading from or writing to the file are performed.
These explanations should help you understand the concepts in simple terms. If you need more
details or examples, let me know!