English
Tamil Nadu Board of Secondary EducationHSC Science Class 11

Write with example how will you dynamically initialize objects?

Advertisements
Advertisements

Question

Write with example how will you dynamically initialize objects?

Answer in Brief
Advertisements

Solution

Dynamic initialization of Objects:

When the initial values are provided during runtime then it is called dynamic initialization.
Program to illustrate dynamic initialization
#include
using namespace std;
class X
{
int n;
float avg;
public:
X(int p,float q)
{
n=p;
avg=q;
}
void disp( )
{
cout<<“\n Roll numbe:-” <<n;
cout<<“\nAverage :-“<<avg;
}
};
int main( )
{
int a ; float b;
cout<<“\nEnter the Roll Number”;
cin>>a; .
cout<<“\nEnter the Average”;
cin>>b;
X x(a,b); // dynamic initialization
x.disp( );
return 0;
}
Output:

Enter the Roll Number 1201
Enter the Average 98.6
Roll number:- 1201
Average :- 98.6

shaalaa.com
  Is there an error in this question or solution?
Chapter 14: Classes and objects - Evaluation - Section - C [Page 247]

APPEARS IN

Samacheer Kalvi Computer Science [English] Class 11 TN Board
Chapter 14 Classes and objects
Evaluation - Section - C | Q 2. | Page 247
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×