PT-1 Model Answers
PT-1 Model Answers
Give any two examples of Object Oriented Programming languages. Ans- An object is an instance of a class. It is a real-world entity created from the
class blueprint and it occupies memory. BL-
e) C0.1
Ans- Two examples of Object-Oriented Programming (OOP) languages are: BL- 1
b) CO.1 Syntax-
1
1. C++ ClassName objectName;
2. Java
State any two applications of Object-Oriented Programming.
private, public, protected, etc.
Ans-
Two Applications of Object-Oriented Programming (OOP): Adding new data and functions Adding new data and function
is not easy. is easy.
1. Desktop Software Development BL-
f) OOP is used to build applications like word processors, spreadsheets, and CO.1 Procedural programming does Object-oriented programming
media players where different components can be modeled as objects. 1
not have any proper way of provides data hiding so it
2. Game Development
hiding data so it is less secure. is more secure.
OOP is ideal for creating games where game elements like players,
enemies, and levels can be represented as objects with properties and
behaviors. In procedural programming, Overloading is possible in
overloading is not possible. object-oriented programming.
Object-oriented programming Write a structure of a simple C++ program and explain each part.
Procedural programming follows
follows a bottom-up Ans-
a top-down approach.
approach. BL-
b) Documentation Section CO.1
2
There is no access specifier in Object-oriented programming Linking Section
procedural programming. has access specifiers like (Header files & Namespace)
Definition Section 2
Manipulators in C++ are used to format the output. They are used with cout to control
(Functions,classes,global variables,typedef) how data is displayed (like spacing, alignment, number formatting, etc.).
Main function
Manipulators are defined in the <iomanip> header file.
Figure :Structure of C++ program
1) Documentation Section:
Commonly Used Manipulators:
This section comes first and is used to document the logic of the program that the
programmer going to code.
It can be also used to write for purpose of the program. 1. endl- The endl manipulator has the same functionality as '\n'(newline
Whatever written in the documentation section is the comment and is not compiled character - telling the program to move the cursor to the next line).
by the compiler. 2. setw()-The setw stands for set width which is defined inside the
Documentation Section is optional since the program can execute without them. <iomanip> header file. It is used to change the width of the next
Below is the snippet of the same: input/output field. When it is used in an expression like out << setw(n) or
/*this is code for first program*/ in >> setw(n). Then it sets the width parameter of the in and out stream
2) Linking Section: equal to n.
The linking section contains two parts:
i) Header Files: Syntax- cout << setw(n) << value;
#include <iostream>
This is a preprocessor directive. 3. setfill()-The setfill() manipulator is used to fill empty spaces in output
It tells the compiler to include the standard input/output stream header file. with a specific character when used with setw() (set width).
Required to use cin and cout.
ii) using namespace std: Syntax- cout << setw(n)<<setfill(char)<< value;
Allows direct access to standard C++ library names (like cout, cin, etc.)
Without this line, you'd have to write std::cout 4. setprecision()-The setprecision manipulator is defined inside the
3) Definition Section: <iomanip> header file, which is used to change floating-point precision
The linking section contains two parts: (meaning the number of digits will display for floating-point numbers) or
i)Header Files: set precision.
#include <iostream>
Syntax- cout << setprecision(n) << number;
This is a preprocessor directive.
Program
It tells the compiler to include the standard input/output stream header file.
Required to use cin and cout.
#include <iostream>
#include <iomanip> // For manipulators
ii)namespace: using namespace std;
using namespace std;
int main() {
float price = 99.456;
Allows direct access to standard C++ library names (like cout, cin, etc.)
Without this line, you'd have to write std::cout.
cout << setfill('*') << setw(10) << "Item" << setw(10) << "Price" << endl;
4)main method:
cout << setfill(' ') << setw(10) << "Pen" << setw(10) << fixed << setprecision(2) << price
The main function tells the compiler where to start the execution of the program. The
<< endl;
execution of the program starts with the main function.
All the statements that are to be executed are written in the main function.
return 0;
}
The compiler executes all the instructions which are written in the curly braces {} which
encloses the body of the main function.
Once all instructions from the main function are executed, control comes out of the
main function and the program terminates and no further execution occur.
Functions are declared in the class and defined outside using the scope
resolution operator ::.
Example:
class Student {
public:
void display(); // Function declaration
};
int main() {
What is type casting in C++? Explain its types with example.
// Printing the given text using cout
Ans-
cout << " Computer Engineering ";
Type casting in C++ refers to converting one data type into another. It is useful
return 0; BL-
f) CO.1 when you want to perform operations between different types or control how
} 2
data is interpreted.
Output
Computer Engineering Types of Type casting:
Explanation: In the above program, cout is used to output the text "
Computer Engineering " to the standard output stream. It works in 1. Implicit Type Casting (Automatic Conversion)
conjunction with the insertion operator (<<) to send the specified data to
the output stream. Done automatically by the compiler.
Happens when a smaller data type is converted into a larger data type.
operator (>>) is used along with the object cin for extracting the data from the cout << b; // Output: 5.0
input stream and store it in some variable in the program. 2. Explicit Type Casting (Manual Conversion)
Syntax-cin >> variable;
Done manually by the programmer using casting operators.
For example, if we want to ask user for his/her age, then we can use cin as shown: Syntax: (type)variable or type(variable)
#include <iostream>
Example:
using namespace std;
int main() { float a = 5.75;
int b = (int)a; // float explicitly cast to int
int age;
cout << b; // Output: 5
Program
#include <iostream>
using namespace std;
int main() {
int x = 10;
float y = 3.5;
// Implicit casting
float sum = x + y; // int x is converted to float
cout << "Sum (implicit): " << sum << endl;
// Explicit casting
int result = x + (int)y; // float y is cast to int
cout << "Sum (explicit): " << result << endl;
return 0;
}
Output:
Sum (implicit): 13.5
Sum (explicit): 13