Professional Documents
Culture Documents
OOP Kit
OOP Kit
OOP Kit
(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 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 2 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 3 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 4 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 5 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
d) Develop c++ program to open and read content of file also write 4M
"object oriented" string in fi1e and close it'
Ans. #include<iostream> Correct
logic 2M
#include<fstream>
using namespace std;
int main() Correct
{ syntax 2M
Page 6 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Description
2M
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. 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
Page 7 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 8 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example
1M each
Page 9 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example:
class Result: public Student
{
float percentage;
};
Page 10 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Protected:
When a base class is inherited by derived class in protected visibility
mode, public and protected members of the base class become
protected members of the derived class.
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.
Example:
class Result: protected Student
{
float percentage;
};
d) Differentiate between Compile time polymorphism and Runtime 4M
polymorphism
Ans. Sr. Compile time Runtime polymorphism Any four
No. polymorphism differences
1M each
1 The linking of function call The linking of function call
to function definition is done to function definition is done
at compile time. at run time.
2 Functions to be executed are Function to be executed is
known well before(at unknown until appropriate
compile time) selection is made at run time
3 This does not require use of This requires use of pointers
pointers to objects to object
4 Function calls execution are Function calls execution are
faster slower
5 It is implemented with It is implemented with
operator overloading or virtual function.
function overloading
Page 11 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Correct
definition of
class
Ans. #include <iostream> Employee
1M,
using namespace std;
class Employee Correct
{ definition of
protected: class
char Name[20]; Customer
1M,
int Id;
long contact_noE; Correct
}; definition of
class Customer class
{ Company
1M,
protected:
char Name[20]; Correct
char Address[50]; definition of
long contact_noC; main() 1M
};
class Company:publicEmployee,public Customer
{
char Name[20];
char location[20];
public:
void getE()
{
cout<<"\nEnter employee's data:";
cout<<"\nName:";
cin>>Name;
cout<<"\nId:";
cin>>Id;
cout<<"\nContact no:";
cin>>contact_noE;
cout<<"\nCompany name:";
cin>>Name;
Page 12 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 13 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 14 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
for(i=0;i<5;i++)
{
cout<<"Enter name of student "<<i+1;
cin>>s[i].Name;
cout<<"Enter Roll Number of student "<<i+1;
cin>>s[i].Roll_No;
cout<<"Enter percentage of student "<<i+1;
cin>>s[i].percentage;
}
cout<<"\nStudents data is: ";
for(i=0;i<5;i++)
{
Page 15 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
if(fin.eof())
cout<<"[EoFreached]\n";
else
cout<<"[error reading]\n";
return 0;
}
5. Attempt any TWO of the following: 12
a) Define class book with following data member and member 6M
functions for 10 book.
Page 16 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Declare
class with
data
members
Ans. #include <iostream.h> and member
class book functions-
{ 4M
char name[20],author[20];
int price;
public: Main
void getdata() function
{ with
cout<<"Enter name"; function
call in loop -
cin>>name; 2M
cout<<"Enter author";
cin>>author;
cout<<"Enter price";
cin>>price;
}
void putdata()
{
cout<<endl<<"Name = "<<name;
cout<<endl<<"Author = "<<author;
cout<<endl<<"Price = "<<price;
}
};
void main()
{
book b[10];
int i;
for(i=0;i<10;i++)
b[i].getdata();
for(i=0;i<10;i++)
b[i].putdata();
getch();
}
Page 17 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
public:
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1; cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<"\t Marks Obtained\n";
cout<<"\n\t Part1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};
class sports: public virtual student
Page 18 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
public:
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=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\t Total Score:"<<total;
}
};
void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
Page 19 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Program:
#include<string.h>
class operatorplus
{
char str1[10];
public:
void getdata()
{
cout<<"\nEnter a string";
cin>>str1;
}
void operator + (operatorplus s)
Page 20 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 21 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
OR
~Test()
{
cout<<"\n No. of Object destroyed:\t"<<count;
--count;
}
};
void main()
{
Page 22 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 23 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Ans.
(Any other correct implementation shall be considered.) Each class
#include<iostream.h> implementat
#include<conio.h> ion-1M
class HOD
{
protected:
char name[10];
char deptName[20];
};
class Faculty:public HOD
{
char fname[10];
public:
void getfaculty()
{
cout<<"Enter department name:";
cin>>deptName;
cout<<"Enter HOD name:";
cin>>name;
Page 24 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 25 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 26 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Faculty f;
Student s;
Technical t;
NonTechnical n;
clrscr();
f.getfaculty();
s.getstudent();
t.getlabT();
n.getlabN();
f.putfaculty();
s.putstudent();
t.putlabT();
n.putlabN();
getch();
}
Page 27 / 27
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Dynamic initialization
Syntax –
int a;
Page 1 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example-
int a;
cout<<”Enter value of variable a”;
cin>>a;
Page 2 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Types:
1. Compile time polymorphism Types 1M
Page 3 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example: Example
2M
class example
{
int a,b;
public:
example()
{
a=0;
b=0;
}
example(int x)
{
a=x;
}
example(int x,int y)
{
a=x;
b=y;
}
};
void main()
{
example I1;
example I2(10);
example I3(10,20);
}
Page 4 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 5 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Example: Example
#include<iostream.h> 2M
class sample
{
int a;
public:
void setdata(int x)
{
this ->a=x;
}
void putdata()
{
cout<<this ->a;
}
};
void main()
{
sample s;
s.setdata(100);
s.putdata( );
}
In the above example, this pointer is used to represent object s when
setdata() and putdata ( ) functions are called.
Page 6 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
void main()
{ Declaration
1M, Display
int n, sum = 0; of natural
clrscr(); numbers
cout<< "Enter a positive integer:"; 1M,
cin>> n;
cout<<"\nNatural numbers till "<<n<<" numbers are:"; Calculate
and display
for(inti = 1; i<= n; i++) sum 2M
{
cout<<i<<" ";
}
cout<<"\n";
for (i = 1; i<= n; i++)
{
sum=sum+i;
}
cout<< "Sum of numbers is = " << sum;
getch();
}
b) Compare static and non static data members (any four points) 4M
Ans.
Static data member Non static data member Any four
correct
1.It is declared using keyword 1.It is declared without using points 1M
'static'. keyword 'static'. each
2. Static data member is 2. Non-static data member
automatically initialized with can be initialized with zero or
zero. non-zero value.
3.All objects of a class share 3.Each object of the class gets
the same copy of static data its own copy of non-static
members. data members.
4.It can be accessed through 4.Itcan be accessed only
static member function as well through non-static member
as non-static member function function of the class
of the class
5. Declaration Syntax: 5.Declaration Syntax:
static data_type variable; data_type variable;
e.g. static int count; e.g. int no;
Page 7 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 8 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Description: -
1. Include header files Correct
In this section a programmer includes all header files which are explanation
required to execute a given program. The most important file is 2M
iostream.h header file. This file defines most of the C++statements
like cout and cin. Without this file one cannot load the C++
program.
2. Class Declaration
In this section a programmer declares all classes which are
necessary for the given program. The programmer uses general
syntax of creating class.
Page 9 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 10 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
class Grandparent
{
};
class Parent1:virtual public Grandparent
{
};
class Parent2:virtual public Grandparent
{
};
class Child: public Parent1,public Parent2
{
};
Example:
#include<iostream.h>
#include<conio.h>
class student
{
intrno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"\n\n\t Roll No:"<<rno<<"\n";
}
};
class test: virtual public student
{
public:
int part1,part2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1; cout<<"Part2:";
Page 11 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
cin>>part2;
}
void putmarks()
{
cout<<"\t Marks Obtained\n";
cout<<"\n\t Part1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};
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=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\t Total Score:"<<total;
}
};
void main()
{
result obj;
Page 12 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
Page 13 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
fstreamFileName;
FileName.open("FileName.txt", ios::in); File object
creation 1M,
if (!FileName) opening of
{ file 1M,
cout<<"File doesn’t exist"; closing of
} file 2M
else
{
cout<<"File opened successfully";
}
FileName.close();
return 0;
}
5. Attempt any TWO of the following: 12M
a) State the use of scope resolution operator and explain it with 6M
example.
Ans. Scope of a variable extends from the point of declaration to the Explanation
3M
end of the block.
A variable declared inside a block is 'local' variable and a
variable declared outside block is called as global variable.
When we want to use a global variable but also has a local
variable with same name.
To access the global version of the variable, C++ provides scope
resolution operator.
Example:-
#include <iostream> Example
3M
using namespace std;
char a = 'm';
static int b = 50;
int main() {
char a = 's';
cout<< "The value static variable b is : "<< ::b;
cout<< "\nThe value of local variable a is : " << a;
cout<< "\nThe value of global variable a is : " << ::a;
return 0;
}
Note: Any other relevant example shall be considered.
Page 14 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 15 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 16 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
{
D obj1; //object of derived class D
obj1.sum();
return 0;
}
Page 17 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 18 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
{
height = ht;
}
protected:
int breadth;
int height;
};
class Rectangle: public Figure
{
public:
int Area()
{
return (breadth * height);
}
};
Page 19 / 20
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
Page 20 / 20