2006 Delhi:
2.c) Define a class named ADMISSION in C++ with the following descriptions:
Private Members:
AD_NO integer(Ranges 10 – 2000)
NAME Array of characters(String)
CLASS Character
FEES Float
Public Members:
Function Read_Data( ) to read an object of ADMISSION type. Function Display( ) to display the details of an object. Function Draw-Nos.( ) to choose 2 students randomly. And display the details. Use random function to generate admission nos. to match with AD_NO.
Ans:
class ADMISSION
{ int AD_NO;
char NAME[31];
char CLASS;
float FEES;
public:
void Read_Data( )
{ cout<<"\nEnter the Admission Number: ";
cin>>AD_NO;
cout<<"\nEnter the Student Name: ";
gets(NAME);
cout<<"\nEnter the Class: ";
cin>>CLASS;
cout<<"\nEnter the Fees: ";cin>>FEES;
}
void Display()
{ cout<<"\nThe Admission Number of the student: "<<AD_NO;
cout<<"\nThe name of the Student: “ <<NAME;
cout<<"\nThe Class of the Student:” <<CLASS;
cout<<"\nThe Fees of the Student: “ <<FEES;
}
void Draw_Nos();
};
void ADMISSION::Draw_Nos( )
{ //Dear Students, a test for you. Complete this member function.
}
2006 Outside Delhi:
1.b) Illustrate the use of Inline function in C++ with the help of an example.
2
Ans: INLINE FUNCTIONS: The inline functions are a C++ enhancement designed to speed up programs. The coding of normal functions and inline functions is similar except that inline functions definitions start with the keyword inline.
The working of inline functions:
After writing any program, it is first compiled to get an executable code, which consists of a set of machine language instructions. When this executable code is executed, the operating system loads these instructions into the computer’s memory, so that each instruction is stored in a specific memory location. Thus, each instruction has a particular memory address.
After loading the executable program in the computer memory, these instructions are executed step by step. When a function call instruction is encountered, the program stores the memory address of the instruction
immediately following the function call statement, loads the function being called into the memory, copies argument values, jumps to the memory location of the called function, executes the function code, stores the return value of the function, and then jumps back to the address of the instruction that was saved just before executing the called function.
With inline code, the compiler replaces the function call statement with the function code itself (this process is called expansion) and then compiles the entire code. Thus, with inline functions, the compiler does not have to jump to another location to execute the function, and then jump back as the code of the called function is already available to the calling program.
Inline functions run a little faster than the normal functions as function calling overheads are saved, however there is a memory penalty. If 10 times an inlinefunction is called, there will be 10 copies of the function inserted into the code.
A function can be declared inline by placing the keyword inline before it. An inline function definition should be placed above all the functions that call it. The functions should be inlined only when they are small. Since for large functions, they will become memory penalty. The inlining does not work for following situations:
a. For functions that return values and are having a loop or a switch or a goto.
b. For functions not returning values, if a return statement exists.
c. If functions contain static variables.
d. If the function is recursive(a function that calls itself).
Inlining and the member functions:
The member function of a class, if defined within the class definition, are inlined by default. Therefore, only very small member functions should be defined within the class definition.
The member functions defined outside the class definition can be made explicitly inline by placing the keyword inline before their definition.
Inline functions are best for small functions that are called often.The compiler may even ignore your attempt to linline a function if it consists more than 50 lines of code.
CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )