1 Oop
1 Oop
1 Oop
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 1 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Diagram
1M
Page 2 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example
1M
Page 3 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
a class definition. Since all the objects belonging to that class use the
same member functions, no separate space is allocated for member Descript
functions. When the objects are created only space for member ion 2M
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.
Diagram
2M
Page 4 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 5 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
program.
6. It is easy to partition the work in a project based on objects.
7. The data-centered design approach enables us to capture more
details of a model in implementable form.
8. Object-oriented systems can be easily upgraded from small to
large systems.
9. Message passing techniques for communication between objects
makes the interface descriptions with external systems much
simpler.
10. Software complexity can be easily managed.
d) Describe ‘this’ pointer with an example. 4M
Ans. ‘this’ pointer:
C++ uses a unique keyword called „this‟ to represent an object that
invokes a member function. This unique pointer is automatically
passed to a member function when it is invoked. „this‟ is a pointer
that always point to the object for which the member function was Descript
called. ion 2M
For example, the function call A.max ( ) will set the pointer „this‟ to
the address of the object A. Then suppose we call B.max ( ), the
pointer „this‟ will store address of object B.
Example:
#include<iostream.h>
class sample
{
int a; Correct
public: example
void setdata(int x) 2M
{
this ->a=x;
}
void putdata()
{
cout<<this ->a;
}
};
void main()
{
sample s;
Page 6 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
s.setdata(100);
s.putdata( );
}
In the above example, this pointer is used to represent object s when
setdata ( ) and putdata ( ) functions are called.
3. Attempt any THREE of the following: 12
a) Write the applications of object oriented programming. 4M
Ans. Applications of object oriented programming are:
1) Real time systems
2) Simulation and modeling Any
3) Object-oriented databases four
4) Hypertext, hypermedia and expertext correct
5) AI and expert systems applicati
6) Neural networks and parallel programming ons 1M
7) Decision support and office automation systems each
8) CIM/CAM/CAD systems
b) State the rules for writing destructor function. 4M
Ans. Rules for writing destructor function are:
1) A destructor is a special member function which should destroy
the objects that have been created by constructor. Any
2) Name of destructor and name of the class should be same. four
3) Destructor name should be preceded with tilde (~) symbol. correct
4) Destructor should not accept any parameters. rules
5) Destructor should not return any value. 1M each
6) Destructor should not be classified in any types.
7) A class can have at most one destructor.
c) What is inheritance? Give different types of inheritance. 4M
Ans. Inheritance:
The mechanism of deriving new class from an old/existing class is
called inheritance. Correct
OR explanat
Inheritance is the process by which objects of one class acquired the ion of
properties of objects of another classes. inherita
nce 2M
Syntax:
Page 7 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Types of inheritance:
1) Single inheritance: In single inheritance, a derived class is
derived from only one base class.
Diagram:
Correct
types of
inherita
nce
2) Multiple inheritance: In multiple inheritance, derived class is (any 4)
derived from more than one base classes. 2M
Diagram:
Page 8 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 9 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 10 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 11 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example:
Program to interchange values of two integer numbers using
friend function.
#include<iostream.h>
#include<conio.h>
class B; Correct
class A example
{ 2M
int x;
public:
void accept()
{
cout<<"\n Enter the value for x:";
cin>>x;
}
friend void swap(A,B);
};
class B
{
int y;
public:
void accept()
{
cout<<"\n Enter the value for y:";
cin>>y;
}
friend void swap(A,B);
};
void swap(A a,B b)
Page 12 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
{
cout<<"\n Before swapping:";
cout<<"\n Value for x="<<a.x;
cout<<"\n Value for y="<<b.y;
int temp;
temp=a.x;
a.x=b.y;
b.y=temp;
cout<<"\n After swapping:";
cout<<"\n Value for x="<<a.x;
cout<<"\n Value for y="<<b.y;
}
void main()
{
A a;
B b;
clrscr();
a.accept();
b.accept();
swap(a,b);
getch();
}
d) Write a program to count the number of lines in file. 4M
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<fstream.h> Opening
#include<conio.h> of file
void main() 1M
{
ifstream file; Countin
char ch; g
int n=0; number
clrscr(); of lines
file.open("abc.txt"); 2M
while(file) Printing
{ number
file.get(ch); of lines
if(ch=='\n') in a file
n++; 1M
Page 13 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
}
cout<<"\n Number of lines in a file are:"<<n;
file.close();
getch();
}
5. Attempt any TWO of the following: 12
a) Write a program to declare a class ‘student’ having data 6M
members as ‘stud_name’ and ‘roll_no’. Accept and display this
data for 5 students.
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<conio.h>
class student
{
int roll_no;
char stud_name[20]; Class
public: declarati
void Accept(); on 2M
void Display();
};
void student::Accept() Accept
{ ( )1M
cout<<"\n Enter student‟s name and roll no\n";
cin>>stud_name>>roll_no;
}
void student::Display()
{ Display
cout<<stud_name<<”\t”<<roll_no<<”\n”; ( ) 1M
}
void main()
{
student S[5];
inti;
clrscr();
for(i=0;i<5;i++)
{ Main ( )
S[i].Accept(); with
} array
cout<<”Student details \n Student‟s Name \t Roll No\n”; 2M
Page 14 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
for(i=0;i<5;i++)
{
S[i].Display();
}
getch();
}
b) State and explain the visibility modes used in inheritance. 6M
Ans. Visibility modes:
private
protected
public
Base class Derived class visibility
visibility Private Protected Public
Private Not Not Not
Inherited Inherited Inherited
Protected Private Protected Protected
Public Private Protected Public
Private:
o When a base class is privately inherited by a derived class,
„public members‟ and „protected members‟ of the base class
become „private members‟ of the derived class.
o Therefore, the public and protected members of the base class
can only be accessed by the member functions of derived class
but, cannot be accessed by the objects of the derived class.
Syntax: Explana
class derived: private base tion 2M
{ for each
//Members of derived class; visibility
}; mode
Public:
o When a base class is publicly inherited by a derived class then
„protected members‟ of base class becomes „protected
members‟ and ‟public members‟ of the base class become
„public members‟ of the derived class.
o Therefore the public members of the base class can be
accessed by both the member functions of derived class as well
Page 15 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Protected:
o When a base class is protectedly inherited by a derived class,
„public and protected members‟ of the base class become
„protected members‟ of the derived class.
o Therefore the public and protected members of the base class
can be accessed by the member functions of derived class as
well as the member functions of immediate derived class of it
but they cannot be accessed by the objects of derived class
Syntax:
class derived: protected base
{
//Members of derived class;
};
c) Write a program to declare a class ‘book’ containing data 6M
members as ‘title’, ‘author-name’, ‘publication’, ‘price’. Accept
and display the information for one object using pointer to that
object.
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<conio.h>
class book
{ Class
char author_name[20]; declarati
char title[20]; on 2M
char publication[20];
float price;
public:
void Accept();
void Display();
};
void book::Accept() Accept
{ ( ) 1M
Page 16 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream fs; File
ofstream ft; open
char ch, fname1[20], fname2[20]; and
cout<<"Enter source file name with extension (like files.txt) : "; close
gets(fname1); 2M
fs.open(fname1);
Page 17 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
if(!fs)
{
cout<<"Error in opening source file..!!";
getch();
exit(1); Logic
} for copy
cout<<"Enter target file name with extension (like filet.txt) : "; contents
gets(fname2); 4M
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();
}
b) Write a program to implement the following hierarchy using 6M
suitable member functions. Refer Figure No.2.
Page 18 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
void display_test()
{
cout<<”\n test Marks \n Marks1 \t Marks2 \n”;
cout<<marks1<<”\t”<<marks2;
}
};
class sports
{
int score;
Page 19 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
public:
void read_sportsData()
{
cout<<”\n Enter sport score\n”; Class
cin>> score; sports
} declarati
void display_sportsData() on 1M
{
cout<<”\n sport score:”<<score;
}
};
class result: public test, public sports
{
int total;
public:
void read_result()
{
read_ studentData () ;
read_test(); Class
read_sportsData(); result
total=marks1+marks2; declarati
} on 2M
void display_result()
{
display_ studentData () ;
display_test();
display_sportsData();
cout<<”\n Total=”<<total;
}
};
void main()
{
result r;
clrscr();
r.read_result(); Main ()
r.display_result(); 1M
getch();
}
Page 20 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 21 / 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 1 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 2 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 3 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 4 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
}
friend void smallest(class1 no1,class2 no2);
};
class class2
{ Definitio
int no2; n of
public: class2
void get2() 1M
{
cout<<"Enter number 2:";
cin>>no2;
}
friend void smallest(class1 no1,class2 no2);
};
void smallest(class1 c1,class2 c2)
{ Friend
if(c1.no1<c2.no2) function
cout<<"no1 is smallest"; 1M
else
cout<<"no2 is smallest";
}
void main()
{ Main()
class1 c1; function
class2 c2; 1M
clrscr();
c1.get1();
c2.get2();
smallest(c1,c2);
getch();
}
Page 5 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
#include<iostream.h>
#include<conio.h>
class STUDENT
{
int Roll_No;
char Name[20];
float Marks;
public:
void Accept();
void Display();
};
void STUDENT::Accept()
Page 6 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
{
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();
}
b) Accept data for five students and display it. Write a C++ 4M
program to displya sum of array elements of array size n.
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<conio.h>
void main()
{
int arr[20],i,n,sum=0;
Page 7 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
clrscr();
cout<<"\nEnter size of an array:"; Initializ
cin>>n; ation of
cout<<"\nEnter the elements of an array:"; array
for(i=0;i<n;i++) 2M
{
cin>>arr[i];
} Calculat
for(i=0;i<n;i++) ion and
{ display
sum=sum+arr[i]; of sum
} of array
cout<<"\nArray elements are:"; elements
for(i=0;i<n;i++) 2M
{
cout<<arr[i]<<" ";
}
cout<<"\nSum of array elements is:"<<sum;
getch();
}
c) Describe with examples, passing parameters to base class 4M
constructor and derived class constructor by creating object of
derived class.
Ans. When a class is declared, a constructor can be declared inside the
class to initialize data members. When a base class contains a
constructor with one or more arguments then it is mandatory for the
derived class to have a constructor and pass arguments to the base
class constructor. When both the derived and base classes contain Correct
constructors, the base constructor is executed first and then the Descript
constructor in the derived class is executed. The constructor of ion 2M
derived class receives the entire list of values as its arguments and
passes them on to the base constructors in the order in which they are
declared in the derived class.
Page 8 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example:
#include<iostream.h>
#include<conio.h>
class base
{
int x; Correct
public: example
base(int a) 2M
{
x=a;
cout<<"Constructor in base x="<<x;
}
};
class derived: public base
{
int y;
public:
derived(int a,int b):base(a)
{
y=b;
cout<<"Constructor in derived.y="<<y;
}
};
void main()
{
clrscr();
derived ob(2,3);
getch();
}
In the above example, base class constructor requires one argument
and derived class constructor requires one argument. Derived class
constructor accepts two values and passes one value to base class
constructor.
Page 9 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Correct
diagram
for
memory
allocatio
n of
objects
2M
Page 10 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 11 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
cout<<"\nSubject 1 marks:"<<m1;
cout<<"\nSubject 2 marks:"<<m2;
cout<<"\nTotal is:"<<Total;
}
};
void main()
{
Result r; main
clrscr(); function
r.accept(); 1M
r.calculate();
r.display();
getch();
}
b) Describe following terms: Inheritance, data abstraction, data 4M
encapsulation, dynamic binding.
Ans. 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. Correct
Data abstraction: descripti
1. Data abstraction refers to the act of representing essential features on 1M
without including the background details or explanations. each
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.
Page 12 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 13 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
ifstream file;
int s=0; Correct
char ch; logic 2M
clrscr();
file.open("abc.txt");
while(file) Correct
{ syntax
file.get(ch); 2M
if(ch==' ')
{
s++;
}
}
cout<<"\nNumber of spaces in text file are:"<<s;
getch();
}
e) Differentiate between contractor and destructor. 4M
(Note: Contractor shall be considered as Constructor.)
Ans. Constructor Destructor
A constructor is a special A destructor is a special
member function whose task is member function whose task is
to initialize the objects of its to destroy the objects that have
class. been created by constructor. Any
It constructs the values of data It does not construct the values four
members of the class. for the data members of the correct
class. differen
It is invoked automatically It is invoked implicitly by the ces 1M
when the objects are created. compiler upon exit of a each
program/block/function.
Constructors are classified in Destructors are not classified in
various types such as : any types.
Default constructor
Parameterized constructor
Copy constructor
Overloaded constructor
A class can have more than one A class can have at the most one
constructor. constructor.
Page 14 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Syntax: Syntax:
classname() destructor name is preceded
{… with tilde.
… ~classname()
} {….
….
}
Example: Example:
ABC() ~ABC()
{ {
… ….
} }
5. Attempt any TWO of the following: 12
a) (i) Write any three rules of operator overloading. 6M
(ii) Write a program in C++ to overload unary ‘_’ operator to
negate values of data members of class.
Page 15 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 16 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
-N1;
cout<<"\n After negation:";
N1. display ();
getch();
}
#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)
{ Correct
cout<< "file not found"; logic
} 3M
else
{ Correct
fout<<fin.rdbuf(); Syntax
} 3M
char ch;
f.seekg(0);
while (f)
{
f.get(ch);
cout<< ch;
}
f.close();
return 0;
}
Page 17 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Output:
Hello World
c) Write a C++ program to declare a class student with members as 6M
roll no, name and department. Declare a parameterized
constructor with default value for department as ‘CO’ to
initialize members of object. Initialize and display data for two
students.
(Note: Any other relevant logic should be considered).
Ans. #include<iostream.h>
#include<conio.h>
#include<string.h>
class student Class
{ student
int roll_no; 1M
char name[20],department[40];
public:
student(int rno,char *n,char *d="CO") Constru
{ ctor
roll_no=rno; definitio
strcpy(name,n); n with
strcpy(department,d); default
} value
void display() 2M
{
cout<<"\n Roll No:"<<roll_no; Display
cout<<"\n Name:"<<name; function
cout<<"\n Department:"<<department; definitio
} n 1M
};
void main()
{
student s1(112," Chitrakshi"),s2(114,"Anjali");
clrscr(); Main
s1.display(); function
s2.display(); definitio
getch(); 2M
}
Page 18 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
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 Descript
for given program. The programmer uses general syntax of creating ion 2M
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.
Page 19 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 20 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Accept and display data for one object of class result (Hint: use
virtual base class).
Page 21 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 22 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
{
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();
}
Page 23 / 23
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 1 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 2 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 3 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 4 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 5 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 6 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example:- Example
int a[5]={10,20,30,40,50},*ptr; 1M
ptr=a[4];
for(i=0;i<5;i++)
{
cout<<*ptr;
ptr- -;
}
Page 7 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
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 Descript
like cout and cin. Without this file one cannot load C++ program. ion 2M
2. Class Declaration
In this section a programmer declares all classes which are necessary
for given program. The programmer uses general syntax of creating
class.
3. Member Functions Definition
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. Main Function Program
In this section programmer creates objects and calls various functions
writer within various class.
Page 8 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
Page 9 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 10 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example:
#include<iostream.h> Relevant
#include<conio.h> example
class test 2M
{
Page 11 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Accept and display data of one teacher and one student using
object of class ‘Info’
Note: Any other correct logic of multiple inheritance in program
shall be considered.
Page 12 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
#include<iostream.h>
Ans #include<conio.h> Correct
class Teacher definitio
{ n of
protected: class -
char Name[20]; Teacher
int empid; 1M
};
class Student Correct
{ definitio
protected: n of
char sname[20]; class-
int rollno; Student
}; 1M
class Info:public Teacher,public Student
{ Correct
public: definitio
void acceptT() n of
{ class-
cout<<"\nEnter data for teacher:"; Info
cout<<"\nName:"; 1M
cin>>Name;
cout<<"\nEmployee id:";
cin>>empid;
}
void displayT()
{
cout<<"\nTeacher's data is:";
cout<<"\nName:"<<Name;
cout<<"\nEmployee id:"<<empid;
}
void acceptS()
{
cout<<"\nEnter student's data:";
cout<<"\nName:";
cin>>sname;
Page 13 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
c) Write a C++ program to swap two integer numbers and swap two 4M
float numbers using function overloading.
Page 14 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 15 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 16 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Ans #include<iostream.h>
#include<conio.h> Creating
#include<string.h> Class
class opov 2M
{
char str1[10];
public: Operato
void getdata() r
{ Functio
Page 17 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 18 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 19 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Ans (i) Write a C++ program to find whether the entered number is
even or odd.
Acceptin
#include<iostream.h> g
#include<conio.h> Number
void main() 1M
{
int num; Conditio
clrscr(); n to
cout<<"\nEnter a Number "; check
cin>>num; number
if(num%2==0) 1M
{
cout<<"\nEntered number is even"; Display
} result
else 1M
{
cout<<"\nEntered number is odd";
}
getch();
}
#include<iostream.h> Creating
#include<conio.h> structur
Page 20 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Accept and display data for one programmer and one manager.
Make display function virtual.
Ans. #include<iostream.h>
#include<conio.h>
class Employee
{
int empid,empcode;
public: Creating
void emp() all
{ classes
cout<<"\nEnter an employee id "; 3M
cin>>empid;
cout<<"\nEnter an employee code ";
cin>>empcode;
Page 21 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 22 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
getch();
}
Accept and display data for one car with all details.
Ans #include<iostream.h>
#include<conio.h>
class Carmanufacturer
{ Declarat
char Name[10];
Page 23 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
void putcarm()
{
cout<<"\nThe Car Name is "<<Name;
}
};
class Carmodel : public Carmanufacturer
{
char Modelname[10];
int Modelno;
public:
void getcarmodel()
{
cout<<"\nEnter Car Model Name and Model No. ";
cin>>Modelname>>Modelno;
}
void putcarmodel()
{
cout<<"\nEnter Car Model Name and Model No.
"<<Modelname<<" "<<Modelno;
}
};
class Car: public Carmodel
{
char colour[10], Carno[10];
public:
void getcar()
{
cout<<"\nEnter Car colour and car number";
cin>>colour>>Carno;
}
void putcar()
{
Page 24 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 25 / 25