0% found this document useful (0 votes)
14 views27 pages

Nishant Oop

The document outlines key characteristics of constructors and benefits of object-oriented programming (OOP), including automatic invocation and data hiding. It also discusses parameterized constructors, virtual functions, access specifiers, and provides various C++ programming examples. Additionally, it differentiates between OOP and procedural programming, and explores concepts like friend functions, scope resolution operator, and visibility modes in inheritance.

Uploaded by

aniketsomankar75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views27 pages

Nishant Oop

The document outlines key characteristics of constructors and benefits of object-oriented programming (OOP), including automatic invocation and data hiding. It also discusses parameterized constructors, virtual functions, access specifiers, and provides various C++ programming examples. Additionally, it differentiates between OOP and procedural programming, and explores concepts like friend functions, scope resolution operator, and visibility modes in inheritance.

Uploaded by

aniketsomankar75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

5 - State any four characteristics of constructor

Constructors are an essential part of object-oriented programming


languages like Java, C++, and Python. Here are four key
characteristics of constructors:
1. Name Same as Class: Constructors are methods within a class that
have the same name as the class itself. This allows the constructor to
be automatically invoked when an object of that class is created.
variables of the object, ensuring that the object is in a valid and usable
state upon creation.
3. No Return Type: Constructors do not have an explicit return type.
In languages like Java and C++, constructors do not specify a return
type (not even 'void'). Their primary purpose is to set up the object,
not to return a value.
4. Automatic Invocation: Constructors are automatically invoked
when an object is created using the 'new' keyword. The appropriate
(based on parameters) is called, and memory is allocated for the
object. Constructors ensure that necessary setup is done before the
object can be used.

Write any four benefits of OOP. 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.
4. It is possible to have multiple instances of an object to co-exist without any
interference.
What is parameterized constructor?
A constructor that accepts parameters is called as parameterized constructor. In
some applications, it may be necessary to initialize the various data members of
different objects with different values when they are created. Parameterized
constructor is used to achieve this by passing arguments to the constructor
function when the objects are created.
Example: class ABC
{
int m;
public:
ABC(int x)
{
m=x;
}
void put()
{
cout<<m;
}
};
void main()
{
ABC obj(10);
obj.put();
}
In the above example, constructor ABC (int x) is a parameterized constructor
function that accepts one parameter. When „obj‟ object is created for class
ABC, parameterized constructor will invoke and data member m will be
initialized with the value 10 which is passed as an argument. Member function
put ( ) displays the value of data member „m‟.

Write a program to count the number of lines in file.


#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main(){
ifstream file;
char ch;
int n=0;
clrscr();
file.open("abc.txt");
while(file) {
file.get(ch);
if(ch=='\n')
n++; }
cout<<"\n Number of lines in a file are:"<<n; file.close(); getch(); }
Write a ‘C++’ program to find factorial of given number using
loop.
#include<iostream.h>
#include<conio.h>
void main()
{
int no,fact=1,i;
clrscr();
cout<<"Enter number:";
cin>>no;
for(i=1;i<=no;i++)
{
fact=fact*i;
}
cout<<"Factorial ="<<fact;
getch();
}

Write a C++ program to find out whether the given number is even or odd
(taking input from keyboard).
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
clrscr();
cout<<"\nEnter a Number ";
cin>>num;
if(num%2==0)
{
cout<<"\nEntered number is even";
}
else
{
cout<<"\nEntered number is odd";
}
getch();
}

Explain the rules of virtual function.


1. The functions cannot be static
2. You derive them using the “virtual” keyword
3. Virtual functions in C++ needs to be a member of some other class (base
class)
4. They can be a friend function of another class
5. The prototype of these functions should be the same for both the base and
derived class 6. Virtual functions are accessible using object pointers
Write a C++ program for write into a file using file operations.
#include<iostream.h>
#include<fstream.h>
#include <conio.h>
int main( )
{
fstream newfile;
newfile.open(“Test.txt”,ios::out);
if(!newfile)
{
cout<<”\nFile cannot be created”;
}
else
{
cout<<”\nFile created successfully”;
newfile<<” Writing this to a file.”;
newfile.close( );
}
getch( );
return 0;
}

Write a C++ program to copy data from one file to another


#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string line;
ifstream srcfile("source.txt");
ofstream destifile("destination.txt");
if(srcfile && destifile)
{
while(getline(srcfile,line))
{
destifile <<line << "\n";
}
cout << "Copy Finished \n";
}
else
{
cout<<"Cannot read File";
}
srcfile.close();
destifile.close();
return 0;
}
Differentiate between constructor and destructor in C++.

Constructor Destructor
Constructor helps to initialize the Whereas destructor is used to destroy
object of a class and allots the the instances.
memory to an object.
Constructors can take arguments Destructors do not take any
arguments.
Constructors can be overloaded. Destructors cannot be overloaded.

In the context of constructors, a class In the context of destructors, a class


can have multiple constructors. always has only one destructor.

Describe ‘this’ pointer in C++ with an example.


In C++, the "this" pointer is a keyword that refers to the object for which a member
function is being called. It's a pointer that holds the memory address of the current
object instance. It's implicitly available within non-static member functions of a
class.
Syntax for this pointer is:
this->variable_name=value;
#include<iostream>
/* local variable is same as a member's name */
class Test
{
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x
// hidden by the local variable 'x'
this->x = x;
}
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
Explain the characteristics of Friend function.
1)It is not in the scope of the class to which it has been declared as friend.
2) Since it is not in the scope of a class, it cannot be called using the object of
that class.
3) It can be invoked like a normal function without the help of any object.
6) It has the objects as arguments.
7) The friend function should not be defined inside the class.

Explain the access specifiers in C++. 4 M


Ans There are 3 types of access specifiers:
1. Public
2. Private
3. Protected
1.Public: All the class members declared under the public specifier will be
available to everyone. The data members and member functions declared as
public can be accessed by other classes and functions too. The public members
of a class can be accessed from anywhere in the program using the direct
member access operator (.) with the object of that class.
2. Private: The class members declared as private can be accessed only by the
member functions inside the class. They are not allowed to be accessed directly
by any object or function outside the class. Only the member functions or
the friend functions are allowed to access the private data members of the class.
3. Protected: The protected access modifier is similar to the private access
modifier in the sense that it can’t be accessed outside of its class unless with
the
help of a friend class. The difference is that the class members declared as
Protected can be accessed by any subclass (derived class) of that class as well.

Write a ‘C++’ program to find factorial of given number using


loop.
(Note: Any other correct logic shall be considered)
#include<iostream.h>
#include<conio.h>
void main()
{
int no,fact=1,i;
clrscr();
cout<<"Enter number:";
cin>>no;
for(i=1;i<=no;i++)
{
fact=fact*i;
}
cout<<"Factorial ="<<fact;
getch();
}
Write a C++ program to create a class STUDENT
The data members of STUDENT class.
Roll_No
Name
Marks
#include<iostream.h>
#include<conio.h>
class STUDENT
{
int Roll_No;
char Name[20];
float Marks;
public:
void Accept();
void Display();
};
void STUDENT::Accept(){
cout<<"\nEnter data of student:";
cout<<"\nRoll number:";
cin>>Roll_No;
cout<<"\nName:";
cin>>Name;
cout<<"\nMarks:";
cin>>Marks;
}
void STUDENT::Display()
{
cout<<"\nStudents data is:";
cout<<"\nRoll number:"<<Roll_No;
cout<<"\nName:"<<Name;
cout<<"\nMarks:"<<Marks;
}
void main()
{
STUDENT S[5];
int i;
clrscr();
for(i=0;i<5;i++)
{
S[i].Accept();
}
for(i=0;i<5;i++)
{
S[i].Display();
}
getch();
}
Write a C++ program to count number of spaces in text file.
(Note: Any other correct logic shall be considered)
Program:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{ ifstream file;
int s=0;
char ch;
clrscr();
file.open("abc.txt");
while(file)
{
file.get(ch);
if(ch==' ')
{
s++;
}
}
cout<<"\nNumber of spaces in text file are:"<<s;
getch();
}

State the difference between OOP and POP.

OBJECT ORIENTED PROCEDURE ORIENTED


PROGRAMMING (OOP) PROGRAMMING (POP)
1 Focus is on data Focus is on doing
rather than things (procedure).
procedure.
2 Programs are Large programs are
divided into multiple divided into multiple
objects. functions.
3 Data is hidden and Data move openly
cannot be accessed around the system
by external functions. from function to
function.
4 Objects Functions transform
communicate with data from one form
each other through to another by calling
function. each other.
5 Employs bottom-up Employs top-down
approach in program approach in program
design design.
(i) Describe structure of C++ program with diagram.
(ii) Write a C++ program to add two 3 x 3 matrices and display
addition.
(i) Describe structure of C++ program with diagram.
INCLUDE HEADER FILES
DECLARE CLASS
DEFINE MEMBER FUNCTIONS
DEFINE MAIN FUNCTION
Description:-
1. Include header files
In this section a programmer include all header files which are
require to execute given program. The most important file is
iostream.h header file. This file defines most of the C++statements
like cout and cin. Without this file one cannot load C++ program.
2. Declare Class
In this section a programmer declares all classes which are necessary
for given program. The programmer uses general syntax of creating
class.
3. Define Member Functions
This section allows programmer to design member functions of a
class. The programmer can have inside declaration of a function or
outside declaration of a function.
4. Define Main Functions
This section the programmer creates object and call various functions
writer within various class.
(ii) Write a C++ program to add two 3 x 3 matrices and display
addition.
(Note: Any other relevant logic should be considered).
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();int mat1[3][3], mat2[3][3], i, j, mat3[3][3];
cout<<"Enter matrix 1 elements :";
for(i=0; i<3; i++)
{ for(j=0; j<3; j++) { cin>>mat1[i][j];
}} cout<<"Enter matrix 2 elements :";
for(i=0; i<3; i++) {
for(j=0; j<3; j++) { cin>>mat2[i][j];
}}
cout<<"Adding the two matrix to form the third matrix\n";
for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
mat3[i][j]=mat1[i][j]+mat2[i][j];
}}
cout<<"The two matrix added successfully...!!";
cout<<"The new matrix will be :\n";
for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
{ cout<<mat3[i][j]<<" ";
}cout<<"\n"; } getch(); }
Write a program to swap two integers using call by reference
method.
(Note: Any other relevant logic should be considered).
#include<iostream.h>
#include<conio.h>
void swap(int*p, 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();
}

Write a program to overload the ‘—’ unary operator to negate the values. (Note: Any
other correct logic shall be considered) #include<iostream.h>
#include<conio.h>
#include<string.h>
class Number {
int x,y;
public:
Number (int a, int b) {
a =x;
b =y; }
void display() {
cout<<"value of x=”<<x<<”\n Value of y= ”<<y;
}
void operator - ( ) {
x = - x; y = - y;
} };
void main ()
{
Number N1(5,6);
clrscr ();
N1. display ();
-N1; cout<<"\n After negation:";
N1. display (); getch (); }
Write a program that copies contents of one file into another file. (Note: Any other
correct logic shall be considered) Assuming input file to be copied file1.txt contents are
“Hello Friends…” and file where the contents need to copy is file2.txt already created
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{ clrscr();
ifstream fs;
ofstream ft;
char ch, fname1[20], fname2[20];
cout<<"Enter source file name with extension (like files.txt) : ";
gets(fname1);
fs.open(fname1);
if(!fs)
{ cout<<"Error in opening source file..!!";
getch();
exit(1);
}
cout<<"Enter target file name with extension (like filet.txt) : ";
gets(fname2);
ft.open(fname2);
if(!ft)
{
cout<<"Error in opening target file..!!";
fs.close();
getch();
exit(2);
}
while(fs.eof()==0)
{
fs>>ch; ft<<ch; }
cout<<"File copied successfully..!!";
fs.close();
ft.close();
getch(); }

What is a class? Give its example.


Class is a user defined data type that combines data and functions together. It is a collection
of objects of similar type.
Example: class Student
{
int rollno;
char name[10];
public:
void getdata( );
void putdata( ); };
Explain use of scope resolution operator. It is used to uncover a hidden variable. Scope
resolution operator allows access to the global version of a variable. The scope resolution
operator is used to refer variable of class anywhere in program. :: Variable_name

State and describe visibility modes and its effects used in


inheritance.
(Note: Diagram is optional)
Different visibility modes are:
1. Private 2. Protected 3. Public
Effects of visibility modes in inheritance:
Private members of base class are not inherited directly in any
visibility mode.
1. Private visibility mode
In this mode, protected and public members of base class become
private members of derived class.
2. Protected visibility mode
In this mode, protected and public members of base class become
protected members of derived class.
3. Public visibility mode
In this mode, protected members of base class become protected
members of derived class and public members of base class
become public members of derived class

Differentiate between C and C++. (Any two points) 2 M


Ans
Sr.
No.
C C++
1 C supports the procedural style
programming. C++ supports both procedural and object oriented.

2 Data is less secured in C. In C++, you can use modifiers for class members to
. make it inaccessible for outside users.
3 C follows the top-down approach. C++ follows the bottom-up approach.

4 C does not support function


overloading. C++ supports function overloding

Explain any four applications of OOP. 2 M


Ans Applications of object-oriented programming are:
1) Real time systems
2) Simulation and modeling
3) Object-oriented databases
4) Hypertext, hypermedia and expertext
5) AI and expert systems
Write a program to implement multiple inheritance as shown in
following Figure No.1:

Program:
#include<iostream.h>
#include<conio.h>
class Subject1 { protected: float m1;
}; class Subject2 { protected: float m2; };
class Result:public Subject1,public Subject2
{
float Total;
public:
void accept()
{
cout<<"Enter marks of subject1:";
cin>>m1;
cout<<"\nEnter marks of subject2:";
cin>>m2;
} void calculate() {
Total=(m1+m2); }
void display() {
cout<<"\nSubject 1 marks:"<<m1;
cout<<"\nSubject 2 marks:"<<m2;
cout<<"\nTotal is:"<<Total;
}
};
void main()
{
Result r;
clrscr();
r.accept();
r.calculate();
r.display();
getch();
}
Write a C++ program to find the area of rectangle using class rectangle
which has following detailsi)
Accept length and breadth from user.
ii) Calculate the area
iii) Display the result.
Ans #include<iostream>
#include<conio.h>
using namespace 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();
}

11 -What is the significance of scope resolution operator?


The scope resolution operator (::) is a fundamental feature in C++ (and some
other programming languages) that serves to identify and access elements, such
as variables, functions, or classes, within a specific scope. Its primary
significance lies in disambiguating and providing explicit access to elements
that are defined within different scopes.
Describe ‘this’ pointer in C++ with an example.
In C++, the "this" pointer is a keyword that refers to the object for which a member
function is being called. It's a pointer that holds the memory address of the current
object instance. It's implicitly available within non-static member functions of a
class.
Syntax for this pointer is:
this->variable_name=value;
#include<iostream>
/* local variable is same as a member's name */
class Test
{
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x
// hidden by the local variable 'x'
this->x = x;
}
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
State the use of cin and cout.
cin: cin is used to accept input data from user (Keyboard).
cout:cout is used to display output data on screen.

Give syntax and use of fclose ( ) function. Syntax: int fclose(FILE* stream); Use: This function
is used to close a file stream. The data that is buffered but not written is flushed to the OS and all
unread buffered data is discarded.
Explain virtual base class with an example.
Virtual Base Class: An ancestor class is declared as virtual base class to avoid
duplication of inherited members inside child class due to multiple paths of
inheritance.

Consider a hybrid inheritance as shown in the above diagram. The child class has
two direct base classes, Parent1 and Parent2 which themselves have a common
base class as Grandparent. The child inherits the members of Grandparent via
two separate paths. All the public and protected members of Grandparent are
inherited into Child twice, first via Parent1 and again via Parent2. This leads to
duplicate sets of the inherited members of Grandparent inside Child class. The
duplication of inherited members can be avoided by making the common base
class as virtual base class while declaring the direct or intermediate base classes
as shown below.
Syntax:
class Grandparent
{
};
class Parent1:virtual public Grandparent
{
};
class Parent2: public virtual Grandparent
{
};
class Child: public Parent1,public Parent2 {
};
Example:
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
int roll_no;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>roll_no;
}
void putnumber()
{
cout<<"\n\n\t Roll No:"<<roll_no<<"\n";
}
};
class test: virtual public student
{
public:
int test1,test2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Test 1:";
cin>>test1; cout<<"Test 2:";
cin>>test2;
}
void putmarks()
{
cout<<"\t Marks Obtained\n";
cout<<"\n\t Test 1 Marks :"<<test1;
cout<<"\n\t Test 1 Marks:"<<test2;
} };
class sports: public virtual student
{ public:
int score;
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore() {
cout<<"\n\t Sports Score is:"<<score; } };
class result: public test, public sports
{
int total;
public:
void display()
{
total=test1+test2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\t Total Score:"<<total;
}};
int main(){
result obj;
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
return 0; }
3-Differentiate between POP and POP
OOP. (any four points (4M) OOP
Object oriented. Structure oriented.
Program is divided into objects. Program is divided into functions.
Bottom-up approach. Top-down approach.
Inheritance property is used. Inheritance is not allowed.
It uses access specifier. It doesn’t use access specifier.
Encapsulation is used to hide the No data hiding.
data.
Concept of virtual function. No virtual function.

6 - Differentiate between structure Structure


and class Class
1. Members of a class are private by 1. Members of a structure are public
default. by default.
2. An instance of a class is called an 2. An instance of structure is called
‘object’. the ‘structure variable’.
3. Member classes/structures of a 3. Member classes/structures of a
class are private by default but not all structure are public by default.
programming languages have this
default behavior eg Java etc.
4. It is declared using the class 4. It is declared using the struct
keyword. keyword.
5. It is normally used for data 5. It is normally used for the grouping
abstraction and further inheritance. of data

5 - State any four characteristics of constructor


Constructors are an essential part of object-oriented programming languages like Java,
C++, and Python. Here are four key characteristics of constructors:
1. Name Same as Class: Constructors are methods within a class that have the same
name as the class itself. This allows the constructor to be automatically invoked when
an object of that class is created.
2. Initialization: Constructors are primarily used for initializing the newly created
object's state or attributes. They set initial values to the instancevariables of the object,
ensuring that the object is in a valid and usable state upon creation.
3. No Return Type: Constructors do not have an explicit return type. In languages like
Java and C++, constructors do not specify a return type (not even 'void'). Their
primary purpose is to set up the object, not to return a value.
4. Automatic Invocation: Constructors are automatically invoked when an object is
created using the 'new' keyword. The appropriate constructor (based on parameters) is
called, and memory is allocated for the object. Constructors ensure that necessary
setup is done before the object can be used.
10 - What is copy constructor?
A copy constructor is a special constructor in object-oriented programming languages like C+
+ that is used to create a new object as a copy of an existing object. It is particularly used for
creating a new object with the same attributes (member variables) and values as an existing
object. Copy constructors are essential for maintaining object-oriented principles and
ensuring proper object duplication.
The copy constructor takes an object of the same class as a parameter and uses it to initialize
a new object. This process involves creating a new instance of the class and copying the
values of the attributes from the source object to the new object.

20 - Define constructor overloading


In C++, We can have more than one constructor in a class with same name, as long as each
has a different list of arguments.This concept is known as Constructor Overloading and is
quite similar to function overloading
Overloaded constructors essentially have the same name (exact name of the class) and
different by number and type of arguments.
A constructor is called depending upon the number and type of arguments passed.
While creating the object, arguments must be passed to let compiler know, which constructor
needs to be called.
21 - List any four properties of constructor function
• Constructor is a member function of a class, whose name is same as the class name. •
Constructor is a special type of member function that is usedto initialize the data
members for an object of a class automatically, when an object of the same class is
created. • Constructor is invoked at the time of object creation. It constructs the values
i.e. provides data for the object that is why it is known as constructor. • Constructor do
not return value, hence they do not have a return type.
22- List four types of constructors
1- Default constructor: It is a constructor that is automatically provided by the
programming language when no explicit constructor is defined in a class. It initializes
the object's variables with default values.
2 - Parameterized constructor: It is a constructor that accepts parameters or arguments
during object creation. It allows the programmer to initialize the object's variables
with specific values supplied at the time of instantiation.
3 - Copy constructor: It is a constructor that allows creating a new object by copying
the values of an existing object. It initializes a new object with the same values as
another object.
4 - Private constructor: It is a constructor that is declared as private, restricting its
access to the class itself. This type of constructor is commonly used in singleton
design patterns, where only one instance of the class is allowed to be created.

23 - Differentiate
between constructor
and destructor S. No. Constructor Destructor

1. Constructor helps to Whereas destructor is


initialize the object of a used to destroy the
class. instances.
2. It is declared as Whereas it is declared
className( arguments as ~ className( no
if any ){Constructor’s arguments ){ }.
Body }.
3. Constructor can either While it can’t have any
accept arguments or arguments.
not.
4. A constructor is called It is called while object
when an instance or of the class is freed or
object of a class is deleted.
created.

14 - Write any four rules to define constructor in a class


1. Same Name as Class: Constructors must have the same name as the class they
belong to. This naming convention ensures that the constructor is automatically called
when an object of the class is created. 2. No Return Type: Constructors do not have a
return type, not even void. Their primary purpose is to initialize the object's state, and
they cannot return a value.
3. Overloading Constructors: You can define multiple constructors in a class, differing
in the number or types of parameters they accept. This is known as constructor
overloading and allows for different ways to initialize objects of the class.
4. Initialization of Members: Constructors are used to initialize the member variables
(attributes) of the object being created. You should use the constructor's parameters to
set initial values for the members based on the provided arguments.
12 - How do we invoke a constructor?
C++ Constructor basic concept
1. C++ constructors are the special member function of the class which are used to
initialize the objects of that class
2. The constructor of class is automatically called immediately after the creation of the
object.
3. Name of the constructor should be exactly same as that of name of the class.
4. C++ constructor is called only once in a lifetime of object when object is created.
5. C++ constructors can be overloaded
6. C++ constructors does not return any value so constructor have no return type.

Define class and object.


Class:
Class is a user defined data type that combines data and functions
together. It is a collection of objects of similar type.
Object:
It is a basic run time entity that represents a person, place or any item
that the program has to handle.
Write a program to declare a class ‘student’ having data members as ‘stud_name’ and
‘roll_no’. Accept and display this data for 5 students. (Note: Any other correct logic shall be
considered) #include<iostream.h>
#include<conio.h>
class student
{
int roll_no;
char stud_name[20];
public:
void Accept();
void Display();
};
void student::Accept()
{
cout<<"\n Enter student‟s name and roll no\n";
cin>>stud_name>>roll_no;
}
void student::Display()
{
cout<<stud_name<<”\t”<<roll_no<<”\n”;
}
void main()
{
student S[5];
inti;
clrscr();
for(i=0;i<5;i++)
{
S[i].Accept();
}
cout<<”Student details \n Student‟s Name \t Roll No\n”;
for(i=0;i<5;i++)
{
S[i].Display();
} getch(); }

Describe use of static data member.


Use of static data member:
Static data member (variable) is used to maintain values common to
the entire class. Only one copy of static member is created for the
entire class and is shared by all the objects of that class. Its lifetime is
the entire program.

Write the use of ios : : in and ios : : out.


ios::in - It is used as file opening mode to specify open file reading
only.
ios::out- It is used as file opening mode to specify open file writing
only.
Write a program that copies contents of one file into another file.
Assuming input file to be copied file1.txt contents are “Hello Friends…” and file where the
contents need to copy is file2.txt already created #include<iostream.h> #include<conio.h>
#include<fstream.h> #include<stdio.h> #include<stdlib.h>
void main()
{ clrscr();
ifstream fs;
ofstream ft;
char ch, fname1[20], fname2[20];
cout<<"Enter source file name with extension (like files.txt) : ";
gets(fname1);
fs.open(fname1);
if(!fs)
{
cout<<"Error in opening source file..!!";
getch();
exit(1);
} cout<<"Enter target file name with extension (like filet.txt) : ";
gets(fname2);
ft.open(fname2);
if(!ft)
{ cout<<"Error in opening target file..!!";
fs.close();
getch();
exit(2);
} while(fs.eof()==0)
{ fs>>ch; ft<<ch;
} cout<<"File copied successfully..!!";
fs.close();
ft.close();
getch(); }

2- What is Data Abstraction ?


Data abstraction in the context of programming refers to the concept of simplifying complex
systems or objects by focusing on their essential properties and behaviors while hiding
unnecessary details. It involves creating abstract data types with well-defined interfaces,
allowing interactions at a higher level of understanding without revealing internal
implementations

State any four object oriented languages.


Object oriented programming language:
 C++
 Smalltalk
 Object pascal
 java
 Simula
 Ada
9 - .List any four features of POP.
1. Procedure-Centric Approach:
In Procedural Programming, the main focus is on procedures or functions. The program is
organized into a sequence of procedures that perform specific tasks. Each procedure can take
input, process it, and produce output. This approach promotes modularity and reusability by
breaking down the program into smaller, manageable chunks of code.
2. Global Data:
POP often relies on global data that can be accessed by any procedure in the program. Data is
not encapsulated within objects, leading to potential issues with data integrity and security.
Changes made to global data in one part of the program can affect other parts, making it
harder to track and manage changes. 3. Sequential Execution:
In procedural programs, code execution follows a sequential flow. Procedures are executed in
the order they are called, and control flow can be managed using constructs like loops and
conditionals. While this makes the control flow easy to understand, it can become complex in
large programs.
4. Limited Code Reusability:
Code reusability in POP is generally achieved through functions or procedures. However, the
reuse is limited to functions that are explicitly called in different parts of the program. There's
no inherent mechanism for creating high-level abstractions or relationships between different
parts of the code.
10 - List any four object oriented languages.
1. Java: Java is a widely used, platform-independent, and versatile object-oriented
programming language. It's known for its "Write Once, Run Anywhere" capability, which
means that Java programs can run on various platforms without modification.
2. C++: C++ is an extension of the C programming language and is known for its support of
both procedural and object-oriented programming paradigms. It provides features like
classes, objects, inheritance, and polymorphism.
3. Python: Python is a versatile and easy-to-learn programming language with a strong
emphasis on readability. It supports object-oriented programming and provides features like
classes, inheritance, and encapsulation.
4. C# (C Sharp): C# is a language developed by Microsoft and is commonly used for
developing Windows applications and games using the .NET framework. It

Accept data for five students and display it. Write a C++
program to displya sum of array elements of array size n.
#include<iostream.h>
#include<conio.h>
void main() {
int arr[20],i,n,sum=0; clrscr();
cout<<"\nEnter size of an array:";
cin>>n;
cout<<"\nEnter the elements of an array:";
for(i=0;i<n;i++)
{ cin>>arr[i]; }
for(i=0;i<n;i++) {
sum=sum+arr[i]; }
cout<<"\nArray elements are:";
for(i=0;i<n;i++) { cout<<arr[i]<<" ";
} cout<<"\nSum of array elements is:"<<sum;
getch(); }
Describe following terms: Inheritance, data abstraction, data
encapsulation, dynamic binding.
Inheritance:
1. Inheritance is the process by which objects of one class acquire
the properties of objects of another class.
2. It supports the concept of hierarchical classification. It also
provides the idea of reusability.
Data abstraction:
1. Data abstraction refers to the act of representing essential features
without including the background details or explanations.
2. Classes use the concept of abstraction and are defined as a list of
abstract attributes such as size, weight and cost and functions to
operate on these attributes.
Data encapsulation:
1. The wrapping up of data and functions together into a single unit
(called class) is known as encapsulation.
2. By this attribute the data is not accessible to the outside world,
and only those functions which are wrapped in the class can
access it.
Dynamic Binding:
1. Dynamic binding refers to the linking of a procedure call to be
executed in response to the call.
2. It is also known as late binding. It means that the code associated
with a given procedure call is not known until the time of the call
at run-time.
Write a C++ program to append data from abc.txt to xyz.txt file.
(Note: Any other correct logic shall be considered)
Assuming input file as abc.txt with contents "World" and output file
named as xyz.txt with contents "Hello" have been already created.
#include <iostream.h>
#include<fstream.h>
int main()
{
fstream f;
ifstream fin;
fin.open("abc.txt",ios::in);
ofstream fout;
fout.open("xyz.txt", ios::app);
if (!fin) {
cout<< "file not found";
} else {
fout<<fin.rdbuf();
} char ch;
f.seekg(0);
while (f) {
f.get(ch);
cout<< ch;
}
f.close();
return 0; }
Write any four benefits of OOP. 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.
4. It is possible to have multiple instances of an object to co-exist without any interference.

2 - Explain memory allocation for objects


Memory allocation for objects refers to the process of reserving a portion of the computer's
memory to hold the data and state associated with an object created in a programming .
classes in object-oriented programming, and they encapsulate both dataThe process of
memory allocation for objects involves the following steps:
1. Determine Object Size: The size of an object is determined by the size of its attributes and
any additional memory overhead required by the programming language or runtime
environment. This size can be calculated based on the data types and structures used within
the class.
2. Allocate Memory: Once the size of the object is determined, the program requests a block
of memory from the computer's memory management system. This memory is typically
allocated from the heap, which is a region of memory used for dynamic memory allocation.
3. Constructor Execution: After the memory is allocated, the constructor of the class is called
to initialize the object. The constructor sets the initial values for the object's attributes and
performs any other setup tasks required for the object's proper functioning.
4. Object Use: Once the memory is allocated and the constructor is executed, the object is
ready for use. Its attributes can be assigned values, and its methods can be called to perform
actions on the object's data.
9 - Write any two rules to define friend function
To define a friend function in C++ (or similar object-oriented languages), you need to adhere
to certain rules to ensure that the function is granted the necessary access to the private and
protected members of a class. Here are two important rules for defining a friend function:
Declaration Inside the Class: To designate a function as a friend of a class, you need to
declare the function inside the class definition. This declaration indicates that the funthe
private or protected members of the class. This declaration should appear within the class's
body but outside of any other member function or access specifier.
2- Friend Function Definition: After declaring the friend function inside the class, you need
to define the actual implementation of the outside the class. This definition should not include
the friend keyword. The friend function is just like any regular function definition.

11 - Write any two characteristics of static member function


Static member functions in object-oriented programming languages like C++ and Java have
certain characteristics that differentiate them from regular member functions. Here are two
key characteristics of static member functions: 1. No Access to Non-Static Members: Static
member functions are associated with the class rather than with instances of the class. access
to non-static (instance) members, including non-static member variables and non-static
member functions. This is because static functions do not have a "this" pointer pointing to a
specific object instance. They can only access other static members of the class.
2. Invoked using Class Name: Static member functions are called using the class name, not
through object instances object to invoke a static member function. Instead, you can directly
use the class name followed by the scope resolution operator (::) to call the function.
Write a C++ program to implement following in heritance. Refer
Figure No.2:
Accept and display data for one object of class result (Hint: use
virtual base class)

# include <iostream.h>
#include<conio.h>
class College_Student
{
int student_id;
char College_code[5];
public:
void read_collegeStud_Data()
{
cout<<”Enter college code and student id\n”;
cin>>college_code>>student_id;
}
void display_collegeStud_Data()
{
cout<<”\ncollege code\tstudent id\n”;
cout<<college_code<<”\t”<<student_id<<”\n”;
}
};
class test: virtual public College_Student
{
float percentage;
public:
void read_test()
{
cout<<”\n Enter test percentage\n”;
cin>> percentage;
}
void display_test()
{
cout<<”\n test percentage:”<<percentage;
}
};
class sports: virtual public College_Student
{
char grade[5];
public:
void read_sportsData(){
cout<<”\n Enter sport grade\n”;
cin>> grade;
}
void display_sportsData()
{
Cout<<”\n sport grade:”<<grade;
}
};
class result: public test, public sports
{
public:
void read_result()
{
read_collegeStud_Data() ;
read_test()
read_sportsData();
}
void display_result()
{
display_collegeStud_Data() ;
display_test()
display_sportsData();
}
};
void main()
{
result r;
clrscr();
r.read_result();
r.display_result();
}

You might also like