theoryprob

Upload: abeer-s

Post on 07-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 TheoryProb

    1/5

    Problems (Structures, Storage Classes, Classes):i. Differentiate between Automatic and Static variables.ii. What do you know about external variables?iii. Why use of external variables is discouraged?iv. What do you mean by life time and scope of an identifier?v. What is a structure?vi. Describe the syntax for defining the composition of a structure.

    vii. What do you mean by constructor?viii. What do you mean by destructor?ix. Discuss different types of constructor.x. What is the difference between setter functions and getter functions?

    xi. Differentiate between a structure and a class.xii. What do you mean by interface of a class?xiii. What do you mean by member functions of a class?xiv. What do you mean by separation of Interface from the implementation?xv. What do you mean by constructor overloading?xvi. What do you mean by utility functions?xvii. What do you mean by visibility in context to class?

    xviii. Differentiate between constructor and destructor.

    Question 1:In C programming, a static variable declared outside of any function is global, but local to the

    current module. A static variable declared within a function retains its value between function calls.#include

    Example code void test1( void ){

    int count = 0 ; // this is local variable printf( "\ntest1 count = %d " , ++count );}

    void test2( void ){ static int count = 0 ; // this is static variable printf( "\ntest2 count = %d " , ++count );}

    int main( void ){ int i; for (i = 0 ; i < 5 ; i++ ) { test1(); test2(); }

    return 0 ;}

    Outputtest1 count = 1

    test2 count = 1test1 count = 1test2 count = 2test1 count = 1test2 count = 3test1 count = 1test2 count = 4test1 count = 1test2 count = 5

    Question 2:

    Where a global variable is declared in one file, but used by functions from another, then the variable iscalled an external variable in these functions, and must be declared as such. The declaration must be

    preceeded by the word extern. The declaration is required so the compiler can find the type of thevariable without having to search through several source files for the declaration.

  • 8/6/2019 TheoryProb

    2/5

  • 8/6/2019 TheoryProb

    3/5

    Question 6Please find attached pdf

    Question 7 to 9The constructor's job is to set up the object so that it can be used

    Destructors are less complicated than constructors. You don't call them explicitly (they are called

    automatically for you), and there's only one destructor for each object. The name of the destructor isthe name of the class, preceeded by a tilde (~). Here's an example of a destructor:

    Player::~Player() {strength = 0;agility = 0;health = 0;

    }

    Types:

    Default Constructor

    A constructor that takes no parameters is called a default constructor. Default constructors are invokedwhenever an object is instantiated by using the new operator and no arguments are provided to new.

    Parameterized Constructor

    At times, we will require initializing class members during instantiation and this is the time where parameterized constructor will come into picture. It follows the same rules as default constructor andwill have parameters. Go through following snippet:

    public class MsDotNetHeaven

    {

    public MsDotNetHeaven()

    {

    //A default Constructor

    }

    public MsDotNetHeaven(String strName)

    {

    //A parameterized Constructor having one parameter

    }

    public MsDotNetHeaven(String strFirstName, String strLastName)

    {

    //A parameterized Constructor having two parameters

    }

  • 8/6/2019 TheoryProb

    4/5

  • 8/6/2019 TheoryProb

    5/5

    the class member list. When the function add() is called in the following example, the data variablesa , b , and c can be used in the body of add() .

    class x{public:

    int add() // inline member function add{return a+b+c;};

    private:int a,b,c;

    };

    Question 14In interface we only declare the member function so the implementation of these functions could bedifferent in inherited classes

    Question 15

    although a constructor for a class is a special member function it is still considered a function and likeall functions in C++ its name can be overloaded. This is the practice of using a function of the samename but having different types and/or numbers of parameters:

    int func( int a );double func( double a );int func( int a, int b );

    double func( int a ); // _NOT_ ALLOWED

    In the above examples we have three declarations of a function func. The first two differ in the type of their parameters, the third in the number of parameters. The fourth example is in fact considered to beequivalent to the first, so is not allowed. This is because in C++ the return type does _not_ form part of the set of types considered for differentiating functions having the same name (i.e. overloadedfunctions).

    Question 17The visibility of a property or method can be defined by prefixing the declaration with the keywords

    public , protected or private . Class members declared public can be accessed everywhere. Membersdeclared protected can be accessed only within the class itself and by inherited and parent classes.Members declared as private may only be accessed by the class that defines the member.

    Question 18Already covered