Problems (Structures, Storage Classes, Classes) :: Void Void Int
Problems (Structures, Storage Classes, Classes) :: Void Void Int
i. ii. iii. iv. v. vi. vii. viii. ix. x. xi. xii. xiii. xiv. xv. xvi. xvii. xviii. Differentiate between Automatic and Static variables. What do you know about external variables? Why use of external variables is discouraged? What do you mean by life time and scope of an identifier? What is a structure? Describe the syntax for defining the composition of a structure. What do you mean by constructor? What do you mean by destructor? Discuss different types of constructor. What is the difference between setter functions and getter functions? Differentiate between a structure and a class. What do you mean by interface of a class? What do you mean by member functions of a class? What do you mean by separation of Interface from the implementation? What do you mean by constructor overloading? What do you mean by utility functions? What do you mean by visibility in context to class? 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 <stdio.h> 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; } Output test1 count = 1 test2 count test1 count test2 count test1 count test2 count test1 count test2 count test1 count test2 count
= = = = = = = = =
1 1 2 1 3 1 4 1 5
Question 2: Where a global variable is declared in one file, but used by functions from another, then the variable is called 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 the variable without having to search through several source files for the declaration.
Global and external variables can be of any legal type. They can be initialised, but the initialisation takes place when the program starts up, before entry to the main function. an external variable is a variable defined outside any function block. As an alternative to automatic variables, it is possible to define variables that are external to all functions, that is, variables that can be accessed by name by any function. (This mechanism is rather like Fortran COMMON or Pascal variables declared in the outermost block.) Because external variables are globally accessible, they can be used instead of argument lists to communicate data between functions. Furthermore, because external variables remain in existence permanently, rather than appearing and disappearing as functions are called and exited, they retain their values even after the functions that set them have returned. Example: File 1:
int GlobalVariable; void SomeFunction(); int main() { GlobalVariable = 1; SomeFunction(); return 0; } // implicit definition // function prototype (declaration)
File 2:
extern int GlobalVariable; void SomeFunction() { ++GlobalVariable; } // explicit declaration // function header (definition)
For more please read http://en.wikipedia.org/wiki/External_variable Question 3 Question 4 A variable's scope is the region of code in which the compiler can uniquely resolve its identifier. Within a particular scope, an identifier must be unique. In C++, objects can have local, global (namespace) or class scope. A variable with local scope is visible only within the function or code block in which it is defined. Variables with global scope are visible and can be used through out a program. Variables have global scope (or namespace scope) if they are defined in the part of the code that is outside of any class or function definition. A class definition also delimits a block of code and a scope. Closely related to scope is the topic of lifespan or lifetime. How long does a variable exist? Will it exists for the entire duration of a program or only for the duration of a function or method call? As we will see, an object's lifetime depends largely on its scope. Question 5 Structure is a collection of variables under a single name. Variables can be of any type: int, float, char etc. The main difference between structure and array is that arrays are collections of the same data type and structure is a collection of variables under a single name. struct Games { char Name[80]; char Rating; BOOL Played; long NumberOfKills; }
Question 6 Please find attached pdf Question 7 to 9 The 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 is the 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 invoked whenever 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 and will have parameters. Go through following snippet: public class MsDotNetHeaven { public MsDotNetHeaven() { //A default Constructor }
public MsDotNetHeaven(String strFirstName, String strLastName) { //A parameterized Constructor having two parameters }
//Class members
} For more please read http://www.intap.net/~drw/cpp/cpp07_03.htm Question 10 Getter functions are used to get value of private functions of class, setter functions are used to set the value of private variables of class Question 11 The struct default access type is public. A struct should typically be used for grouping data. The class default access type is private, and the default mode for inheritance is private. A class should be used for grouping data and methods that operate on that data. In short, the convention is to use struct when the purpose is to group data, and use classes when we require data abstraction and, perhaps inheritance. In C++ structures and classes are passed by value, unless explicitly de-referenced. In other languages classes and structures may have distinct semantics - ie. objects (instances of classes) may be passed by reference and structures may be passed by value. Question 12 Interface class only contains functions declarations not implementation
class IDemo { public: virtual ~IDemo() {} virtual void OverrideMe() = 0; }; class Parent { public: virtual ~Parent(); }; class Child : public Parent, public IDemo { public: virtual void OverrideMe() { //do stuff } };
Question 13 Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static; this is called a static member function. A member function that is not declared as static is called a nonstatic member function. The definition of a member function is within the scope of its enclosing class. The body of a member function is analyzed after the class declaration so that members of that class can be used in the member function body, even if the member function definition appears before the declaration of that member in
the class member list. When the function add() is called in the following example, the data variables a, b, and c can be used in the body of add().
class x { public: int add() {return a+b+c;}; private: int a,b,c; };
Question 14 In interface we only declare the member function so the implementation of these functions could be different in inherited classes Question 15 although a constructor for a class is a special member function it is still considered a function and like all functions in C++ its name can be overloaded. This is the practice of using a function of the same name 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 be equivalent 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. overloaded functions). Question 17 The 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. Members declared 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 18 Already covered