Oop Unit I
Oop Unit I
Oop Unit I
com/object-oriented-
programming-using-c-for-msbte-3k-scheme/
Exam
S.N MSBTE Board Asked Questions Marks
Year
State any two features of object oriented programming 2 S-24
1.
State the features of object oriented programming. 4 S-23
S-24
2. Define class and object 2
S- 19
Class:
Class is a user defined data type that combines data and functions together. It is acollection of
objects of similar type.
Object:
It is a basic run time entity that represents a person, place or any item that theprogram has to handle.
1
3. State the use of Memory management operator and explain it with 2 S-24
example.
manually by a programmer.
• Use of dynamically allocated memory is to allocate memory of variable size, which is not
possible with compiler allocated memory except for variable-lengtharrays.
• We are free to allocate and deallocate memory whenever we need it and whenever we don’t need
it anymore.
new operator: This operator is used to allocate memory dynamically at runtime. Ittakes the data
type of the object you want to create as an argument and returns a pointer to the newly allocated
memory block.
delete operator: This operator is used to deallocate memory that was previouslyallocated using
new operator. It takes a pointer to the memory block you want tofree as an argument.
Example:
numbers[i] = i * i;
delete [ ] numbers;
2
4. Develop a program to find factorial of given number using for loop 4 S-24
#include<iostream> using
namespace std;int main()
{
int no, fact=1, i; cout<<"Enter number:";
cin>>no; for(i=1;i<=no;i++)
{
fact=fact*i;
}
cout<<"Factorial ="<<fact;
}
}
5. Develop a program to declare a class student the data members are rollno, 4 S-24
name and marks, accept and display data for one object of class student
#include<iostream> using
namespace std;class student
{
int rollno;
char name[20];float marks;
public:
void accept(); void display();
};
void student::accept()
{
cout<<"\nEnter data of student:";cout<<"\nRoll number:";
cin>>rollno; cout<<"\nName:";
cin>>name; cout<<"\nMarks:";
cin>>marks;
}
void student::display()
{
cout<<"\nStudents data is:"; cout<<"\nRoll
number:"<<rollno;cout<<"\nName:"<<name;
cout<<"\nMarks:"<<marks;
}
int main()
{
student S; S.accept();
S.display();
}
3
6. With suitable example describe structure of C++ program. 4 S-24
#include<iostream> using
int a[3][3],b[3][3],c[3][3];
int i,j;
4
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
for(j=0;j<3;j++)
cin>>b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"\n The addition of two matrices A and B is:";
for(i=0;i<3;i++)
{
cout<<"\n"; for(j=0;j<3;j++)
{
cout<<"\t"<<c[i][j];
}
5
8. Differentiate between C and C++(Any two marks). 2 W-23
C C++
6
9. Give the syntax of class. 2 W-23
Syntax:
class class_name
{
private:
variable declarations;function
declarations;public:
variable declarations;
function declarations;
};
3) Object-oriented databases
CIM/CAM/CAD systems
7
11. Explain the input operator in C++ 2 W-23
The input operator, commonly known as the extraction operator and denoted by >>,is a
powerful tool in C++ used for reading data from input streams. It works in conjunction with the
standard input stream object cin and allows you to easily retrieve various types of data from the
user or any other input source.
Example:
int number;
8
The general rules for naming variables are:
• Variable names are case sensitive (myVar and myvar are different variables).
• Reserved words (like C++ keywords, such as int) cannot be used as variablenames.
Choose variable names that are descriptive and reflect the purpose or meaning of thevariable. This
helps improve code readability and makes it easier for others (including yourself) to understand
the code later.
13. Write a C++ program to find out whether the given number is 4 W-23
even orodd(Taking input from keyboard)
#include <iostream>
using namespace std;
int main() {
int number;
if (number % 2 == 0) {
} else {
return 0;
9
14. Write a C++ program to find the area of rectangle using class rectangle 4 W-23
whichhas following details:
areaiii)Display the
result
#include<iostream>
std;class rectangle
{
private:
int length; int breadth;
public:
void accept()
{
cout<<"Enter length & breadth: \n";cin>>length;
cin>>breadth;
}
void area()
{
int area; area=length*breadth;
cout<<"\nArea of rectangle:"<<area;
}
};
int main()
{
rectangle r;r.accept();
r.area();
getch();
10
15. Demonstrate the static and dynamic initialization of variable. 2 S-23
Static Initialization
In static initialization, the value of a variable is assigned at compile time. Thismeans the
value is fixed and cannot be changed during program execution.
<< std::endl;return 0;
Dynamic Initialization
In dynamic initialization, the value of a variable is assigned at runtime. This allowsfor flexibility
as the value can be determined based on user input, calculations, or other factors.
11
16. Write the syntax for declaration of a class. 2 S-23
namespace std;
return sum;
int n = 5;
12
18. State the use of scope resolution operator and explain it with example. 6 S-23
The scope resolution operator, represented by two colons (::), is primarily used in C++ to
manage the visibility and access of elements within different program scopes. Here's a
breakdown of its functionalities with examples:
1. When a local variable or function shares the same name with a global one, thelocal
element takes precedence within its scope.
2. To access the global element from within the local scope, you use the scoperesolution
operator with the global variable/function name.
someFunction() {
13
std::cout << "Local value: " << value << std::endl; std::cout <<
return 0;
outputs:
Local value: 20
Global value: 10
class MyClass
{
public:
int num = 5;
void printNum() {
std::cout << "Member variable num: " << num << std::endl;
}
};
int main() { MyClass obj;
obj.printNum(); // Accessing member function
std::cout << "Member variable num using scope resolution: " << obj::num
<< std::endl;return 0;
}
outputs:
14
Member variable num using scope resolution: 5
Consider a scenario where you have two namespaces Math and Science, both containing
a function named calculate. To call Science::calculate fromthe main scope, you'd use:
In essence, the scope resolution operator provides a way to explicitly specify the context (global,
class, or namespace) when referring to an identifier, ensuring clarityand avoiding naming conflicts
in your code.
19. Explain user defined datatype with example. 2 W-22
User-Defined DataTypes
The data types that are defined by the user are called the derived datatype or user-defined
derived data type. These types include:
Class
Structure
Union
Enumeration
Typedef
20. Develop a c++ program to print Fibonacci series. 4 W-22
#include <iostream>using
15
for (int i = 1; i <= n; ++i) {
if(i == 2) {
t2 = nextTerm;
cout << nextTerm << ", ";
return 0;
Output
21. Develop a c++ program to print sum of 10 no. using array. 4 W-22
#include <iostream>using
int arr[100],i,size,sum=0;
16
cin>>size;//Accepting array size cout<<"Enter the value of
elements: "<<endl;for(i=0;i<n;i++)
for(i=0;i<n;i++)
sum=sum+arr[i];//Calculating sum
Output:
value of elements:
10
12
22. Define class book with following Data member and member function 6 W-22
for 10book
1. B - name → getdata ( )
17
2. B - author → put data ( )
3. B - price
#include <iostream>using
public:
void getdata()
void putdata()
"<<price;
}
};
for(int i=1;i<=10;i++)B[i].getdata();
for(int i=1;i<=10;i++)B[i].putdata();
return 0;
}
18
23. List two memory management operators available in C++. Also state its 2 S-22
use.
In C++, memory management is primarily handled by two operators:
new: This operator is used for dynamically allocating memory at runtime. Ittakes the
data type as an argument and returns a pointer to the newly allocated memory block.
Example:
C++
delete: This operator is used to deallocate memory that was previously allocated using
new. It takes a pointer to the memory block you want to freeas an argument.
Example:
C++
19
Features of object oriented programming are:
1. Objects: Objects are the fundamental building blocks of OOP. They represent real-
world entities with attributes (data) and methods (functions)that define their
behavior.
2. Classes: A class that defines the properties and methods of an object. Aclass that
from existing classes. This promotes code re-usability and simplifies the creation of
differently to the same message. This makes code more flexibleand adaptable.
7. Dynamic Binding: In dynamic binding, the code to be executed in response tothe function
call is decided at runtime. Because dynamic binding is flexible, it avoids the drawbacks of static
binding, which connected the function call anddefinition at build time.
Message Passing: Objects communicate with one another by sending and receiving information.
A message for an object is a request for the execution of a procedure and therefore will invoke a
function in the receiving object that generates the desired results. Message passing involves
specifying the name of the object, the name of the function, and the information to be sent.
25. Describe concept of type casting using suitable example. 4 S-22
Type casting in C++ refers to the process of converting a value from one data
typeto another. This can be useful in various scenarios, such as:
20
need to add an integer and a floating-point number. In such cases, type casting allows
you to explicitly convert one type to the other to enable theoperation.
Passing arguments to functions: If a function expects a specific data typeas input, you
can cast a variable of a different type to match the function's requirement.
In this example, x (an integer) is implicitly converted to a float before addingto the float
value 3.14f.
You can explicitly tell the compiler to convert a value from one type toanother using
cast operators.
static_cast: This is the most commonly used cast operator for safe conversions
between compatible types. It performs compile-time typechecking.
dynamic_cast: This operator is used for runtime type checking, typicallywhen dealing
with inheritance hierarchies. It's useful for downcasting (converting a base class
pointer to a derived class pointer).
class Animal
{ /* ... */ };
class Dog : public Animal { /* ... */ };
21
const_cast: This cast removes the const-ness of a variable or expression.Use with
caution as it can lead to unexpected behavior if you modify a constant value.
Explicit casting can sometimes lead to data loss (truncation) if the target typecan't hold
the value of the source type.
Use type casting judiciously and consider alternative approaches like using appropriate data types
or functions that handle mixed data types.
26. Write a C++ program to find and display the smallest number of an array. 4 S-22
#include <iostream>using
cout << "Enter the number of elements in the array: ";cin >> n;
< n; ++i) {
arr[0];
cout << "The smallest element in the array is: " << smallest << endl;
return 0;
22
27 State the difference between OOP and POP 2 W-19
functions.
3. Data is hidden and cannot be Data move openly around the
accessedby external functions system from function to
function.
Clanguage.
23
28 What is a class? Give its example. 2 W-19
Class: Class is a user defined data type that combines data and functions together. Itis a
collection of objects of similar type.
Example:
class book
//
};
29 Describe memory allocation for objects. 4 W-19
24
allocated for member functions. When the objects are created only space for member variable is
allocated separately for each object. Separate memory locations for the objects are essential
because the member variables will hold different data values for different objects.
Benefits of OOP:
1. We can eliminate redundant code and extend the use of existing classes.
2. We can build programs from the standard working modules that communicate with one
another, rather than having to start writing the code from scratch. This leads to saving of
development time and higher productivity.
3. The principle of data hiding helps the programmer to build secure programs that cannot be
invaded by code in other parts of the program.
It is possible to have multiple instances of an object to co-exist without any
25
interference.
#include<iostream.h>
#include<conio.h> void main()
{
int arr[20];
int i, j, temp,n;clrscr();
cout<<"\n Enter the array size:";cin>>n;
cout<<"\n Enter array elements:";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<”Sorted Array:”;for(i=0;i<n;i++)
{
cout<<”\n”<<arr[i];
}
getch();
}
26
32. State the use of cin and cout. 2 S-19
33. Write a ‘C++’ program to find factorial of given number using loop. 4 S-19
#include <iostream>using
{
int i,fact=1,number; cout<<"Enter any
for(i=1;i<=number;i++){
fact=fact*i;
Output:
Enter any Number: 5Factorial of 5 is: 120
27
34. Describe following terms: Inheritance, data abstraction, data 4 S-19
encapsulation,dynamic binding.
Inheritance:
idea of reusability.
Data abstraction:
abstract attributes such as size, weight and cost and functions to operate on theseattributes.
Data encapsulation:
1. The wrapping up of data and functions together into a single unit(called class)
is known as encapsulation.
and only those functions which are wrapped in the class can access it.
Dynamic Binding:
2. It is also known as late binding. It means that the code associatedwith a given
at run-time.
28
35. Write a program to swap two integers using call by reference method. 6 S-19
#include<iostream.h>
int*q)
int t; t=*p;
*p=*q;
*q=t;
void main()
{
int a,b; float x,y;
clrscr();
cout<<"Enter values of a and b\n";cin>>a>>b;
cout<<"Before swapping\n";
cout<<"a="<<a<<"\tb="<<b<<endl;swap(&a, &b);
cout<<"After swapping\n";
cout<<"a="<<a<<"\tb="<<b<<endl;getch();
29
36. State any four object oriented languages. 2 W-18
C++
Smalltalk
SimulaAda
Turbo pascalEiffel
C#
Python
37. Write a C++ program to declare a class ‘circle’ with data members as 4 W-18
radius and area. Declare a function getdata to accept radius and putdata
to calculateand display area of circle.
#include<iostream.h>
#include<conio.h>
class circle
{
float radius,area;public:
void getdata()
{
cout<<"Enter radius:";cin>>radius;
}
void putdata()
{
area=3.14*radius*radius; cout<<"Area of
circle="<<area;
}
};
void main()
{
circle c;clrscr();
c.getdata();
c.putdata();
getch();
}
#include<iostream.h>
#include<conio.h> class addition
{
int x,y;public:
30
addition(int,int);void display();
};
void addition::display()
{
cout<<"\nAddition of two numbers is:"<<(x+y);
}
void main()
{
addition a(3,4);a.display();
getch();
}
39. Write a C++ program to print multiplication table of 7. (example: 7 × 1 = 7 4 W-18
.....
7 × 10 = 70)
#include<iostream.h>
#include<conio.h> void main()
{
int num;clrscr();
cout<<"Multiplication table for 7 is:"<<endl;
for(num=1;num<=10;num++)
{
cout<<"7 *"<<num<<"="<<7*num<<endl;
getch();
31
40. Write a C++ program to declare a class ‘Account’ with data members as 6 W-18
accno,name and bal. Accept data for eight accounts and display details of
accounts having balance less than 10,000
#include<iostream.h>
#include<conio.h> class Account
{
long int accno, bal;char name[10];
public:
void getdata()
{
cout<<"\nEnter account number, balance and name ";
cin>>accno>>bal>>name;
}
void putdata()
{
if(bal>10000)
{
cout<<"\nThe Account Number is "<<accno;cout<<"\nThe
Balance is "<<bal; cout<<"\nThe Name is "<<name;
}
}
};
void main()
{
Account a[8];int i;
clrscr(); for(i=0;i<8;i++)
{
a[i].getdata();
}
for(i=0;i<8;i++)
{
a[i].putdata();
}
getch();
32
41. Write a C++ program to find whether the entered number is even or odd. 3 W-18
#include<iostream.h>
#include<conio.h> void main()
{
int num;clrscr();
cout<<"\nEnter a Number ";cin>>num;
if(num%2==0)
else
getch();
Thank You
https://shikshamentor.com/object-oriented-programming-using-c-for-
msbte-3k-scheme/
Visit
https://shikshamentor.com/