You cannot copy content of this page.

Author Archive admin

Constructors and Destructors in C++




Constructors and Destructors in C++

Constructors:

To initialize the data members of a class, Constructors are needed.  A Constructor function is a member function that is invoked automatically when an object of the class is created.  The name of a Constructor function is similar to that of the class name.  It is used to construct data members i.e. provide initial values to the data members of the class.  The Constructors may be invoked implicitly or explicitly.  Since data members are generally defined in private section in a class, they cannot be initialized like structure variables.  So, we need a member function to perform the same job and to invoke this function automatically by the compiler and not by the programmer, its name is kept similar to that of the class.

Consider the following example:

class sample
{
int x;
int y;
public:
sample( ){x=0; y=0;}
void put_ans( )
{
cout<<”x=”<<x;
cout<<”y=”<<y;
}
};

void main()
{
sample obj; //Constructor function is called
//and x,y receive value 0.
obj.put_ans();
}

In the above example, x and y are initialized to zero, when an object obj of class sample is created.  It is confirmed by printing the values through put_ans() function.  Hence x=0 and y=0 gets displayed on the screen.

A default constructor is one, which is invoked automatically and takes no arguments.  A parameterized constructor is one that can take arguments to initialize the data member.  A copy constructor is invoked when an object is initialized with some other object of the same class.




Destructors:

Destructor is a member function that has name similiar to that of class and precedes with a tilde (~) symbol.  A destructor function neither accepts any argument nor does it return any value.  The objects that are created using constructors are destroyed using destructors.  A destructor function is called automatically when the function goes out of scope and the program terminates.  If new operator is used in constructor for allocating memory, then delete operator is used in destructors for deallocating memory.

For example:

class sample{
int x;
int y;
public:
sample()
{
x = 0;
y = 0;
}
void put_ans()
{
cout<<”x = “<<x;
cout<<”y = “<<y;
}
~sample();  // Destructor declaration.
};
sample :: ~ sample()
{
cout << “Destructor function invoked !”;
}

The above example defines a constructor and destructor for the class named sample.  When the program is about to terminate the destructor function is called and “Destructor function invoked !” message gets displayed on the screen.

Tags,

Advantages of reference variables over pointers in C++




Advantages of reference variables over pointers in C++

A reference variable is an alias to a variable already defined. After a reference variable is created, the value can be referred to by the original variable or the reference variable. Whenever we declare a reference variable, we need to initialize it at that moment. Take for example, if an ans is an int type variable, defined as:

int ans = 5;         then
int & result = ans;

result is another name of ans, by which now we can refer to the value 5.

The following assignment

result = 10;

changes the value of both the variables to 10.


To create a reference variable &(ampersand) operator is used, where & in place of representing address represents a reference to the type of variable defined.

A pointer variable is a variable that contains the addresses of some other variable. In case of pointer variables, & operator is used to fetch the address of a memory location and * (asterisk) is the de-referencing operator that fetches the value stored in a pointer variable. One has to take care of such assignments when passing or receiving this value by reference, which are not needed in case of reference variables.  Moreover reference variables are used to pass arguments to a function by reference.

Tags,

Polymorphism in C++




Polymorphism in C++

Polymorphism refers to “One Object Many Forms”. It is one of the most important features of OOPs. Polymorphism is implemented through Overloaded operators and Overloaded functions. Polymorphism can be compile time polymorphism or runtime polymorphism. In C++, polymorphism is implemented through virtual functions and dynamic binding. Early binding refers that an object is bound to its functions call at compile time. Compile time polymorphism is achieved through overloaded functions. The overloaded member functions are invoked as per their argument type and number.  For example:

int simple_interest (int principal, int rate, int time);
float simple interest (float principal, float rate, float time);

The following function call:
simple_interest (1000,5);//invokes first  prototype

Whereas the function call
simple_interest(2000,50,2.50,4.50); //Invokes the second prototype

This information is available during compile time to which function will be invoked with respect to a function call.

Run-time polymorphism is achieved through virtual functions.

For example:

class B
{
public:
virtual void func1();
}

class D : public B
{
public:
void func1();
}

void B : : func1()
{
cout<<”control in base class —-?”;
}

void D : : func1()
{
cout<<”control in derived class—–?”;
}
void main()
{
B b_obj; D d_ojj;
B *b_ptr;
b_ptr = &b_obj;
b_ptr->func1();
b_ptr = &d_obj;
b_ptr->func1();
}

If Base class function is not defined as virtual then the base class pointer always invokes the base class function irrespective of the object being pointed to by the base class pointer. But when the base class function is defined as virtual, then it is determined at run time as to invoke which function and hence is runtime polymorphism.

 

Tags,

Virtual function – Difference between a Virtual function and an Overloaded function




Virtual function

Polymorphism is the property that allows objects of different types to behave differently to the same message. In C++, polymorphism is achieved through virtual functions. Whenever we define a function with the same name in the Base class as well as in the Derived class, the base class pointer, irrespective of pointing to the desired class object, always call the base class function. If the function in the base class is defined as virtual then the function to be invoked is determined at runtime depending upon the case as to what object is being pointed to by the pointer of the base class. This is also known as runtime polymorphism or late binding or dynamic binding.




Virtual function vs. Overloaded function

A Virtual function is different from an Overloaded function. A Virtual function must have similar definitions everywhere i.e. the function prototype defined in the base class must be identical to that of the function defined in the desired class. But if two functions bear same name but different parameter list or different prototype, then they are considered as overloaded functions in place of virtual functions.

 

 

Tags,

DOEACC A LEVEL A6-R4 DATA STRUCTURE THROUGH C++ QUESTION PAPERS



DOEACC A LEVEL

A6-R3/R4 DATA STRUCTURE THROUGH ‘C’ LANGUAGE/’C++’

NOTE:

1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains SIX questions.

2. PART ONE is to be answered in the TEAR-OFF ANSWER SHEET only, attached to the question paper, as per the instructions contained therein. PART ONE is NOT to be answered in the answer book.

3. Maximum time allotted for PART ONE is ONE HOUR. Answer book for PART TWO will be supplied at the table when the answer sheet for PART ONE is returned. However, candidates, who complete PART ONE earlier than one hour, can collect the answer book for PART TWO immediately after handing over the answer sheet for PART ONE.




A6-R3: QUESTION PAPERS

1. A6-R3: Data Structure through ‘C’ Language
[JANUARY 2006 Question paper]
[wpdm_file id=2]
2. A6-R3: Data Structure through ‘C’ Language
[JULY 2006 Question paper]
[wpdm_file id=6]
3. A6-R3: Data Structure through ‘C’ Language
[JANUARY 2007 Question paper]
[wpdm_file id=3]
4. A6-R3: Data Structure through ‘C’ Language
[JULY 2007 Question paper]
[wpdm_file id=7]
5. A6-R3: Data Structure through ‘C’ Language
[JANUARY 2008 Question paper]
[wpdm_file id=4]
6. A6-R3: Data Structure through ‘C’ Language
[JULY 2008 Question paper]
[wpdm_file id=8]
7. A6-R3: Data Structure through ‘C’ Language
[JANUARY 2009 Question paper]
[wpdm_file id=5]
8. A6-R3: Data Structure through ‘C’ Language
[JULY 2009 Question paper]
[wpdm_file id=9]
9. A6-R3: Data Structure through ‘C’ Language
[JANUARY 2010 Question paper]
[wpdm_file id=11]
10. A6-R4: Data Structure through ‘C++’
[JULY 2010 Question paper]
[wpdm_file id=12]
11. A6-R4: Data Structure through ‘C++’ 
[JANUARY 2011 Question paper]
[wpdm_file id=13]
12. A6-R4: Data Structure through ‘C++’
[JULY 2011 Question paper]
[wpdm_file id=14]
13. A6-R4: Data Structure through ‘C++’
[JANUARY 2012 Question paper]
[wpdm_file id=15]
14. A6-R4: Data Structure through ‘C++’
[JULY 2012 Question paper]
[wpdm_file id=16]
15. A6-R4: Data Structure through ‘C++’
[JANUARY 2013 Question paper]
[wpdm_file id=17]

 

Ps:  Please keep on visiting site, as I will  be updating this site for other question papers also.

Tags

Virtual Base class


Virtual Base Class:

A virtual base class is one that is specified as virtual when it is inherited.  If the base class definition precedes the keyword virtual, then it is referred to as virtual base class.  If a situation arises, where the data members of a base class are inherited more than once to a derived class through different inheritance paths, as in the case of hybrid inheritance, the duplication is avoided by defining the base class as virtual.  When a base class is defined as virtual, then only one copy of the members of the base class is inherited, irrespective of the existence of different inheritance paths.

An example demonstrating the use of virtual base class is given below:

Example:

#include <iostream.h>
#include <conio.h>

class teacher
{
protected:
int s_id;
public:
void get_v()
{
cout<<“\nEnter school id>”;
cin>>s_id;
}
void put_v()
{
cout<<“\nSchool id=”<<s_id;
}
};

class science : virtual public teacher
{
protected:
int science_code;
float science_marks;
public:
void get_sc()
{
cout<<“\nEnter science code?”;
cin>>science_code;
cout<<“\nEnter marks in science?”;
cin>>science_marks;
}
void put_sc()
{
cout<<“\nScience code =”<<science_code;
cout<<“\nMarks in Science =”<< science_marks;
}
};

class maths : virtual public teacher
{
protected:
int maths_code;
float maths_marks;
public:
void get_mc()
{
cout<<“\nEnter code for mathematics?”;
cin>>maths_code;
cout<<“\nEnter marks in Maths?”;
cin>>maths_marks;
}
void put_mc()
{
cout<<“\nMaths code =”<<maths_code;
cout<<“\nMarks in Maths =”<<maths_marks;
}
};

class tests_conducted : public science, public maths
{
protected:
float total_marks;
public:
void compute()
{
total_marks = science_marks + maths_marks;
}
void put_info()
{
put_v();
put_sc();
put_mc();
cout<<“\nTotal Marks =”<<total_marks;
}
};

void main()
{
clrscr();
tests_conducted test;
test.get_v();
test.get_sc();
test.get_mc();
test.compute();
test.put_info();
getch();
}

 

Tags,

'inline' keyword

Advantage of using the keyword ‘inline’ before a function:

The member functions of a class may be defined inside the class definition or outside the class using scope resolution operator. The functions defined inside a class are inline by default. Generally, the functions, which are small, are defined inside a class definition.

If a function defined outside the class intends to be inline, then inline keyword is used before the function, as shown below:

class A
{       int x;
public:
void getx();
int square(int);
};

inline int A :: square(int a)
{
return(a*a);
}

With inline keyword the function call will be replaced by the function code by the compiler. But it is upon the wish of compiler to make any function inline or not even after inline keyword is used. Only small functions should be made inline. If the functions are bigger in size, then they should be declarations normal functions. Before calling an inline function, it is required that the function is defined elsewhere.

Tags,

Scope resolution operator in C++

Function of scope resolution operator:

In C++ the scope of a variable is from the point of its declaration till the end of the block in which it is defined. A variable defined outside a block is global whereas the variable defined inside a block is local to the block. If the two variables defined in different scope bear similar name, then the global variable (variable in the outer block) is not accessible from the inner block. To access the global version of the variable, scope resolution operator is used, preceding the variable name.

Consider the following example:-

//some code segment
—————————
—————————
{             float ans=5;
{
float ans = 10;
cont << “ans=”<<ans;
cont <<”ans=”<<::ans;
————————–
}
—————-
—————-
}

The first output statement results in a value 10, which overrides the global value of a variable, whereas the second output statement fetches the global value of the variable, i.e. 5, as scope resolution operator is used.

Tags,

Function Prototype & Function Overloading

Function Prototype:

Function prototype also referred to as function signature specifies the number of arguments and return type of the function to the compiler. Function prototype is required when a function is invoked/called before it is defined. The following statement defines function prototype.

            <Return type> function_name(arguments);

where return type is any valid C++ data type. The arguments are the parameters or values passed to the function when it is invoked. The argument variable names in prototype are optional.

Function Overloading:

Function overloading refers to defining different functions with the same name but with different arguments and return types. This is also referred to as functional polymorphism, as overloading is a form of polymorphism. This function to be invoked is determined by the type and number of arguments passed to that function.

For example: if we have to find the sum of two numbers of type int as well as float type we need not define two different functions with different names.  Now we can define both the functions bearing same name but different argument type.

int add (int a, int b);

float add(float a, float b);

Now the same function ‘add’ sums up two integer values and returns an integer as well as float values, thereby returning float value.

Tags,

Object Oriented Programming


Object Oriented Programming:

Object-Oriented Programming is a program design approach. It is incorporated with several features and new concepts, which are quite different from other programming techniques such as Modular programming, Structural programming or Procedural programming. It supports objects that are data abstraction with an interface of named operations and a hidden local state. It ties data along with their methods that operate on data. It supports data encapsulation feature by encapsulating the data, which cannot be accessed by external functions. Object orientation demands the concept of data encapsulation so that an object does not permit other objects to access its data and operates directly. The services of an object can be called upon through messages, classes are defined that contains the data members along with methods or functions that operate on data. Data Abstraction, which represents only essential features to the outside world, again is a striking feature of OOPs, besides this inheritance supports reusability and extensibility of code segments, polymorphism implemented through function overloading and operation overloading, compile time polymorphism or dynamic binding are some of the other features of OOPs.

It is different from conventional programming approaches, such as Modular programming, Procedural programming, Top-down & Bottom-up programming, Structural programming, as it incorporates powerful features as mentioned to provide extra power to the programming approach. OOPs model the real world objects with great ease, which was quite difficult with conventional programming languages. There was no data security feature and it moved freely around the system. Overloading, Inheritance, Dynamic binding of modules etc. empowered it further and thus proved to be a good programming approach over other programming approaches.

Tags

x Close

Like Us On Facebook