Вы находитесь на странице: 1из 8

Q31. What is the structure of C++ Program? A typical C++ program would contain four sections.

These sections may be placed in separate code files and then compiled independently or jointly. Include files Class declaration Member functions definitions Main function program

It is a common practice to organise a program into three separate files. The class declarations are placed in a header file and the definitions of member functions go into another file. This approach enables the programmer to separate the abstract specification of the interface from the implementation details. Finally the main program that uses the class is placed in a third file which includes the previous two files as well as any other files required. This approach is based on the concept of client server model. The class definition including the member functions constitute the server that provides services to the main program known as client. The client uses the server through the public interface of the class. Q32. What are control statements in C++? Control statement are of three major categories. They are a) Sequence structure: the flow of execution of statements in a program is sequential. b) Decision/ selection statement: A selection between two courses of action can be made by the decision/ selection structure. Two types of selection statements are available in C++ : ifelse and switch. c) Looping: A loop statement is used for repetitive task. The iteration statements are also called loops or looping statements. There are three types of loop : for loop, while loop and do-while loop.

1. Simple if statement Syntax: if(expression is true) { action1; } action2; action3;

2. Ifelse statement Syntax: if(expression is true) { action1; } else { action2; } action3;

3. switch statement This is a multiple-branching statement where, based on condition, the control is transferred to one of many possible points. Syntax: switch(expression) { case1: { action1; } case2: { action2; } . . default: { action3; }

} action5; 4. dowhile statement The do-while is exit control loop. Based on a condition, the control is transferred back to a particular point in the program. Syntax: do { action1; } while (condition is true); action2; 5. while statement It is a loop statement which is entry control. Syntax: while (condition is true) { action1; } action2; 6. for statement This is an entry controlled loop statement and is used when an action is to be repeated for a predetermined number of times. Syntax: for(initial value; test; increment) { action1; } action2;

Q33. Explain passing objects by reference, passing objects by value and passing objects by pointer. Pass by value:

The callee function receives a set of values that are to be received by the parameters. All these copies of values have local scope, i.e., they can be accessed only by the callee function. The simplicity and guarantee of unchanging of values passed are the advantages of pass by value. Pass by reference: The calling function receives a set of references which are aliases to variables. If a change is made to the reference variable, the original value (passed by the caller function) will also be changed. All the references are handled by the pointers. Multiple values modification can be done by passing multiple variables. Pass by pointer: The callingt function receives a pointer to the variable. The value of the pointer in the caller function can then be modified. The advantages of this process are that the changes are passed back to the caller function and multiple variables can be changed. Q34. What is reference variable? A reference variable provides an alias (alternative name) for a previously defined variable. For example, if we make the variable sum a reference to the variable total, then sum and total can be used interchangeably to represent that variable. A reference variable is created as follows: data-type & reference-name=variable-name Example: float total=100; float & sum = total;

total is a float type variable that has already been declared: sum is the alternative name declared to represent the variable total. Q35. What are the characteristics of static data member in C++? A data member of a class can be declared static; be it in the public or private section of the class definition. Such a data member is created and initialized only once, in contrast to non-static data members which are created again and again for each separate object of the class. Static data members are created when the program starts. Note, however, that they are always created as true members of their classes. Public static data members are like `normal' global variables: they can be accessed by all code of the program, simply using their class names, the scope resolution operator and their member names. Q36. Write short note on static data member and static member function. A data member of a class can be qualified as static. A static member variables has certain special characteristics. These are: It is initialized to zero when the first object of its class is created. Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created. It is visible only within the class, but its lifetime is the entire program.

Static variables are normally used to maintain values common to the entire class. For example, a static data member can be used as a counter that records the occurrences of all the objects. A member function that is declared static has the following properties: A static function can have access to only other static members( functions and variables) A static member function can be called using the class name(instead of its object) class-name :: function-name; class test { int code; static int count; public: void setcode() { code =++count; } void showcode() { cout<<Object Number : <<code<<\n; } static void showcount() { cout<<Count : <<count<<\n } }; int test::count; void main() { test t1,t2; t1.setcode(); t1.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t1.showcount; t2.showcount; t3.showcount; } Q37. What are static member functions? ;

A static function can have an access to only other static members (functions or variables) declared in the same class. A static member function can be called using the class name instead of its objects. E.g. Something :: getvalue(); Like static member variables, static member functions are not attached to any particular object. Here is the above example with a static member function accessor: class Something { private: static int s_nValue; public: static int GetValue() { return s_nValue; } }; int Something::s_nValue = 1; // initializer int main() { std::cout << Something::GetValue() << std::endl; } Because static member functions are not attached to a particular object, they can be called directly by using the class name and the scope operator. Like static member variables, they can also be called through objects of the class type, though this is not recommended. Q38. What are the characteristics of static member function? I. II. III. Only static data members can be used. Object names will not be used when calling static member function. Classname will be used when calling static member function.

Q39. How are arguments passed in a function? The arguments passed to a function can be performed in two ways: Passed By Value Passed By Reference

Passed By Value: In the earlier chapter, all examples for the function with arguments were passed by value. Arguments passed by value are the copies of the values of variables and are passed to the function. The variables defined in the calling function are not passed in this manner.

For example: Sample Code 1. int s=5, u=6; 2. int z; 3. z = sum(s,u) would pass values as 1. int sum(int x, int y) 2. z = sum(5,6); Thus, the copies of the values 5 and 6 are passed to the function definition and not the variables s and u. Thus, any changes in the variable x and y would not have any effect or change the variables s and u. Because s and u are not passed in this manner, only copies of the variables are passed to the function definition. Passed By Reference: Passed by reference indicates a contrast in that the variables are not passed by value. When a variable is passed by reference, it passes the variable to the function definition and not the copies of the value. Any changes to the variable in function definition would affect or reflect corresponding changes in the variables passed as reference. The symbol used for denoting the passed by reference is & (ampersand). For Example: Suppose two integer variables x and y are defined in the calling function, the main program with values x=5 and y=4. Suppose the function exforsys receives the value as passed by reference from this function it is defined and called as follows: Sample Code #include <iostream> using namespace std; void main( ) { void exforsys(int&,int&); //Function Declaration - & denotes passed by reference int x=5,y=4; exforsys(x,y); cout << "n The output from Calling program is:"; cout << "n x=" << x; cout << "n y=" << y;

void exforsys(int& s, int& u) //Function Definition - & denotes passed by reference { s=s*10; u=u*10; }

Q39(a). What do you mean by default arguments in functions? C++ allows a function to assign a parameter a default value when no argument corresponding to that parameter is specified in a call to that function. The default value is specified in a manner syntactically similar to a variable initialization. For example, this declares MyFunction as taking one int argument with a default value of 0: Void MyFunction(int i =0) { // // } Now, this MyFunction() can be called one of the following two ways: MyFunction(10); MyFunction(); //passing explicit value //not passing any value; function will give default value

One of the reasons why default arguments are included in C++ is because they provide another method for the programmer to manager greater complexity. To handle the widest variety of situations, quite frequently a function contains more parameters than are required for its most common usage. Thus, when the default arguments apply, you need specify only the arguments that are meaningful to the exact situation, not all those needed by the most general case. Q40. What is a downcast? A downcast is a cast from a base class to a class derived from the base class. A downcast is only safe if the object addressed at runtime is actually addressing a derived class object.

Вам также может понравиться