Introduction To C++ Notes Class 12 Computer Science
Structure-Collection of logically related different data types (Primitive and Derived) referenced under one name.
Nested structure
• A Structure definition within another structure.
• A structure containing object of another structure.
typedef
• Used to define new data type name
#define Directives
• Use to define a constant number or macro or to replace an instruction.
Inline Function
• Inline functions are functions where the call is made to inline functions, the actual code then gets placed in the calling program.
• What happens when an inline function is written?
• The inline function takes the format as a normal function but when it is compiled it is compiled as inline code. The function is placed separately as inline function, thus adding readability to the source program. When the program is compiled, the code present in function body is replaced in the place of function call.
General Format of inline Function:
The general format of inline function is as follows:
inline datatype function_name(arguments)
inline int MyClass( )
Example:
#include <iostream.h>
int MyClass(int);
void main( )
{int x;
cout << “\n Enter the Input Value: ”;
cin>>x;
cout<<”\n The Output is: “ << MyClass(
} inline int MyClass(int x1)
{return 5*x1;}
The output of the above program is:
Enter the Input Value: 10
The Output is: 50
The output would be the same even when the inline function is written solely as a function. The concept, however, is different. When the program is compiled, the code present in the inline function MyClass ( ) is replaced in the place of function call in the calling program. The concept of inline function is used in this example because the function is a small line of code.
The above example, when compiled, would have the structure as follows:
#include <iostream.h>
int MyClass(int);
void main( )
{int x;
cout << “\n Enter the Input Value: ”; cin>>x;
//The MyClass(x) gets replaced with code return 5*x1;
cout<<”\n The Output is: “ << MyClass(x);
}
#include <iostream.h>
int MyClass(int);
void main( )
{int x;
cout << “\n Enter the Input Value: ”;
cin>>x;
//Call is made to the function MyClass
cout<<”\n The Output is: “ << MyClass(x);
}
int MyClass(int x1)
{return 5*x1;}
1 Marks questions
1) Name the header files that shall be needed for the following code:
void main( )
{ char word[]=”Board Exam”;
cout<<setw(20)<<word;
}
2) Name the Header file to which the following built in functions belongs to:
(i) gets() (ii) abs() (iii) sqrt() (iv) open()
2 Marks questions:
1) Rewrite the following program after removing the syntactical error(s) if any. Underline each correction.
#include<iostream.h>
void main( )
{ F = 10, S = 20;
test(F;S);
test(S);
}
void test(int x, int y = 20)
{ x=x+y;
count<<x>>y;
}
2) Find the output of the following program:
#include<iostream.h>
void main( )
{ int U=10,V=20;
for(int I=1;I<=2;I++)
{ cout<<”[1]”<<U++<<”&”<<V 5 <<endl;
cout<<”[2]”<<++V<<”&”<<U + 2 <<endl; } }
3) Rewrite the following C++ program after removing the syntax error(s) if any. Underline each correction. [CBSE 2010]
include<iostream.h>
class FLIGHT
{ Long FlightCode;
Char Description[25];
public
void addInfo()
{ cin>>FlightCode; gets(Description);}
void showInfo()
{ cout<<FlightCode<<”:”<<Description<<endl;}
};
void main( )
{ FLIGHT F;
addInfo.F();
showInfo.F;
}
4) In the following program, find the correct possible output(s)from the options:
#include<stdlib.h>
#include<iostream.h>
void main( )
{ randomize( );
char City[ ][10]={“DEL”, “CHN”, “KOL”, “BOM”, “BNG”};
int Fly;
for(int I=0; I<3;I++)
{Fly=random(2) + 1;
cout<<City[Fly]<< “:”;
}}
Outputs:
(i) DEL : CHN : KOL: (ii) CHN: KOL : CHN:
(iii) KOL : BOM : BNG: (iv) KOL : CHN : KOL:
5) In the following C++ program what is the expected value of Myscore from options (i) to (iv) given below. Justify your answer.
#include<stdlib.h>
#include<iostream.h>
void main( )
{ randomize( );
int Score[ ] = {25,20,34,56,72,63},Myscore;
cout<<Myscore<<endl;
}I
i) 25 (ii) 34 (iii) 20 (iv) Garbage Value.
Answer to Questions
1 Marks Answer
1) Ans: iostream.h
iomanip.h
2) Ans: iostream.h (ii) math.h,stdlib.h (iii) math.h (iv)fstream.h
2 marks Answers
1 Ans:
#include<iostream.h>
void test(int x,int y=20); //Prototype missing
void main( )
{ int F = 10, S = 20; //Data type missing
Text(F,S); //Comma to come instead of ;
Text(S);
void Text(int x, int y)
{ x=x+y;
cout<<x<<y; //Output operator << required }
2 Ans:Output:
[1]10&15
[2]21&13
[1]11&16
[2]22&14
3 Ans: #include<iostream.h>
class FLIGHT
{ long FlightCode;
char Description[25];
public:
void addInfo()
{ cin>>FlightCode; gets(Description);}
void showInfo()
{ cout<<FlightCode<<”:”<<Description<<endl;}
};
void main( )
{ FLIGHT F;
F.addInfo();
F.showInfo;
}
4 Ans)
Since random(2) gives either 0 or 1, Fly value will be either 1 or 2.
Discover more from EduGrown School
Subscribe to get the latest posts sent to your email.