22316_Object Oriented Programming Using C++
22316_Object Oriented Programming Using C++
(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
using namespace std; 3M
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