0% found this document useful (0 votes)
15 views6 pages

PT-1 Model Answers

The document provides an overview of Object-Oriented Programming (OOP) concepts, including classes, objects, inheritance, and access specifiers in C++. It includes syntax for defining classes and creating objects in C++, as well as examples of member functions and manipulators. Additionally, it compares procedural programming with OOP and discusses type casting in C++.

Uploaded by

aryantelang264
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)
15 views6 pages

PT-1 Model Answers

The document provides an overview of Object-Oriented Programming (OOP) concepts, including classes, objects, inheritance, and access specifiers in C++. It includes syntax for defining classes and creating objects in C++, as well as examples of member functions and manipulators. Additionally, it compares procedural programming with OOP and discusses type casting in C++.

Uploaded by

aryantelang264
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/ 6

Both languages support core OOP concepts like classes, objects, inheritance,

polymorphism, encapsulation, and abstraction.


Define a class with syntax in C++.
New Satara College of Engineering & Management (Polytechnic),
Ans-
Korti, Pandharpur
Common Practice Test-I Model answers Class: A class is a user-defined data type,it acts as a blueprint(template) for
(A.Y-2025-26)
creating objects.It contains data members(variables) and member
Programme Name :Computer Engineering 313304
Programme Code : CO3K functions(methods).
Semester : III
Syntax:
Course : Object Oriented Programming Date: 02/08/2025
Using C++ (OOP) class className BL-
Marks: 30 Time: 09:15 AM To 10:45 AM c) { CO.1
access_specifier: 1
Instructions
// data member
(1) All Questions are compulsory.
access_specifier:
(2) Answer each next main Question on a new page.
// member functions
(3) Illustrate your answers with neat sketches wherever necessary. };
(4) Figures to the right indicate full marks. where,
(5) Assume suitable data, if necessary.
(6) Use of Non-programmable Electronic Pocket Calculator is permissible. Data Members: These are the variables that are defined inside the class.
(7) Mobile Phone, Pager and any other Electronic Communication devices are not Member Functions: Functions declared inside a class. Also referred to as a
permissible in Examination Hall.
member method.
CO1. Write C++ programs using classes and objects.
What are access specifiers in C++? List any two.
Ans- Access specifiers in C++ define the access level (visibility) of class members
10 (variables and functions) from outside the class.
Q1. Attempt any FIVE.
Marks
State any two applications of Object Oriented Programming.
1. public
o Members declared as public are accessible from anywhere BL-
Ans- Two applications of Object-Oriented Programming (OOP) are: d) CO.1
(inside or outside the class). 1
o Example: obj.display();
1. Software Development: 2. private
OOP is widely used in developing large-scale software systems such as o Members declared as private are accessible only within the
BL-
a) operating systems, database systems, and business applications due to its CO.1 class.
modularity and code reusability. 1 o Cannot be accessed directly from outside using object.
2. Game Development: o Example: obj.data; is not allowed if data is private.
In games, different objects like players, enemies, and environments can be
modeled using classes and objects, making OOP ideal for building
complex and interactive games.
Write syntax to create an object in C++.

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.

What is the use of scope resolution operator in C++? In object-oriented


In procedural programming,
Ans- The scope resolution operator :: is used in C++ to access something that programming, the concept of
there is no concept of data
data hiding and inheritance is
is outside the current scope, such as: hiding and inheritance.
used.
1. To define a class member function outside the class
Bl-
g) 2. To access a global variable when a local variable has the same name CO.1 In procedural programming, the In object-oriented
1 function is more important than programming, data is more
3. Namespace resolution the data. important than function.
4. Accessing class static members
5. 5.Refer to base class member in derived class Procedural programming is Object-oriented programming is
based on the unreal world. based on the real world.

20 Procedural programming is used Object-oriented programming is


Q2. Attempt any FIVE. for designing medium-sized used for designing large and
Marks
programs. complex programs.
Compare Procedure Oriented Programming and Object Oriented
Programming. Procedural programming uses Object-oriented programming
the concept of procedure uses the concept of data
Procedural Oriented Object-Oriented abstraction. abstraction.
Programming Programming
Code reusability absent in Code reusability present in
In object-oriented procedural programming, object-oriented programming.
In procedural programming, the
programming, the program is BL-
a) program is divided into small CO.1
divided into small parts
parts called functions. 2 Examples: C, FORTRAN, Examples: C++, Java, Python,
called objects.
Pascal, Basic, etc. C#, etc.

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.

Explain the use of manipulators in C++ with suitable examples.


c) CO.1 BL-
Ans- Manipulators in C++:
Write a simple C++ program to input and display student details using class Class: A class is a user-defined data type,it acts as a blueprint(template) for
and object. creating objects.It contains data members(variables) and member
Ans- functions(methods).
#include <iostream> Syntax:
using namespace std;
class className
{
// Define class
access_specifier:
class Student {
// data member
private:
access_specifier:
int rollNo;
// member functions
string name;
};
where,
public:
void getDetails() { Data Members: These are the variables that are defined inside the class.
cout << "Enter Roll Number: ";
Member Functions: Functions declared inside a class. Also referred to as a
cin >> rollNo;
cout << "Enter Name: "; member method.
cin >> name;
}
Defining member functions :
void displayDetails() {
cout << "\n--- Student Details ---\n"; 1. Inside the Class
cout << "Roll Number: " << rollNo << endl; BL-
d) CO.1  Functions are defined directly inside the class body.
cout << "Name: " << name << endl; 3
}  These functions are automatically inline by default.
};
Example:
int main() {
Student s1; // Create object of Student class Student {
public:
s1.getDetails(); // Input details
void display() {
s1.displayDetails(); // Display details cout << "This is a student." << endl;
}
return 0; };
}
2. Outside the Class

 Functions are declared in the class and defined outside using the scope
resolution operator ::.

Example:

class Student {
public:
void display(); // Function declaration
};

// Function definition outside class


void Student::display() {
cout << "This is a student." << endl;
Define a class in C++. Describe how member functions can be defined inside }
BL-
e) and outside the class. CO.1
1
Ans-
Explain basic input/output functions used in C++ with syntax and example . // Taking input from user and store
Ans- // it in variable
Standard Output Stream – cout cin >> age;
The C++ cout is the instance of the ostream class used to produce output on the // Output the entered age
standard output device which is usually the display screen. The data needed to be cout << "Age entered: " << age;
displayed on the screen is inserted in the standard output stream (cout) us ing the return 0;
insertion operator(<<). }
Syntax-cout << value/variable; Output
For example, if we want to print text "Computer Engineering" on the display, we 18 (Enter by user)
Your age is: 18
can use the cout as shown: Explanation: The above program asks the user to input the age. The
Program object cin is connected to the input device (keyboard). The age entered
by the user is extracted from cin using the extraction operator(>>) and
#include <iostream> the extracted data is then stored in the variable age present on the right
using namespace std; side of the extraction operator.

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.

Standard Input Stream - cin BL-


g) Example: CO.1
The C++ cin statement is the instance of the class istream and is used to read 1,2
int a = 5;
input from the standard input device which is usually a keyboard. The extraction float b = a; // int is implicitly converted to float

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

=========@@@@@@All the Best@@@@@@=========

You might also like