You cannot copy content of this page.

Category Archive OOPs

Limitations on Constructors and Destructors in C++




Limitations on Constructors and Destructors in C++

Limitations on Constructors:-

The following are the limitations on constructor functions.

  1. Constructor functions should be declared in the public section of the class.  Private and Protected constructors are available only to the member functions and friends of that class.
  2. A constructor function does not have any return type, not even void.  Hence, constructor functions can’t return any value.
  3. A constructor function may not be static.
  4. We cannot fetch the address of a constructor function.
  5. Constructors cannot be made virtual.
  6. Constructors may not be static.
  7. Constructor functions cannot be inherited.  However, a derived class can call the base class constructor.
  8. An object of a class with a constructor can’t be member of a union.
  9. The copy constructor and the default constructor are generated by the compiler only if these have not been declared for a class.
  10. Name of the constructor function must be same as that of the class.

Limitations on Destructors:

The following are the limitations imposed on destructor functions:

  1. Destructors neither take any argument nor do they return any value, not even void.
  2. Destructors cannot be inherited.
  3. It is not possible to refer to the address of a destructor.
  4. A destructor may not be static.
  5. An object of a class with a destructor cannot be a member of a union.
  6. A destructor function must have public access in the class declaration.
  7. A destructor name must be similar to that of a class, preceded by a tilde (~) symbol.


Tags,

Concept of Data hiding in a class in C++


Concept of Data hiding in a class in C++

In C++, the class groups data members and functions under three access specifiers – private, protected and public, where private and protected members remain hidden from outside world and thereby support data hiding, so that only relevant and required information is exposed to the user and rest of the unnecessary information remains hidden from the user.  The private and protected membes remain hidden from the user and the public members form an interface by providing the relevant and needed information to the outside world, which is nothing but data hiding.  Consider the following example:

class student
{
int roll_no;
public:
int rno;
void get_info()
{
roll_no = 5;
}
void put_info()
{
cout<<”Roll no = “<<roll_no;
}
};
void main(void)
{
student obj;
obj.roll_no = 5;  //results in an error //message-roll_no //not accessible.
obj.rno = 7;  // a valid assignment
obj.get_info();
}

In the above example, when the private variable roll_no is referred to by the object of the same class, it results in an error message, as a private member is accessible to only member functions.  So in order to initialize this private data member, a member function named get_info() is used, that assigns the value 5 to roll_no.  But the second assignment is perfectly a valid assignment in C++ as rno is a public variable.  Hence data members are hidden by specifying them under private section in a class.

 

Tags,

Macro vs. Inline function in C++




Macro vs. Inline function in C++

Functions considerably increase the speed of the program, when it is likely to be called many number of times.  But, if the function is small in size, then it may be defined as inline function which greatly reduces the function calling overheads.  Whenever an inline function is called, the function is inserted in the code itself rather than calling it, as is the case with normal functions.  This insertion is performed during compilation time.  Inline functions are declared like normal functions except that the function precedes with the keyword inline.

 

However, it should be noted that it is up to the wish of the compiler to make a function inline or not, even after inline specification has been given.  Also the inlining procedure will not follow if a function contains static variable or is recursive.  If a function has looping or conditional construct, and it also return value, then the inlining won’t function.


 

Macro is a preprocessor directive.  It is not really a function.  So some overheads are involved in error checking during compilation time.  When a constant is associated with a user defined symbol, that could be used in place of constant, the characters making up the constant are assigned not the numeric value of the constants.  All macros can occupy more than one line but they can not include a newline.  Macros can take arguments too.  The advantages of macros vs. functions depend on the situation.  C++ provides its own facility to define functions as inline, which have same working as that of macros, but, since they are functions, so the compiler checks for the possible errors very easily.  Further, it is upto the compiler whether to make a function inline or not, but macros, without memory consideration are expanded.  Thus in C++, inline functions prove to be much useful then macros.

Tags,

Function scope and File scope in C++




Function scope and File scope in C++

The scope for a function or a variable is determined by the place, where it is declared.  If it is defined within the body of a function then the scope is local to the function or function scope.  For example: if a function named sub() is defined inside another function named super(), then sub() can be accessed only through super().  Hence scope of sub() is local to super().

super()
{
———-
———-
sub()
{
———-
———-
}
}

Here sub() is a local function to super() and is not externally accessible.

If the above declaration of sub() function appears outside super() or all other functions, then it becomes available to all the functions in the file and the scope now is referred to as the file scope.  Thus it is accessible from anywhere in the program.  Such variables and functions which are globally available within a program are global variables and functions.  Hence

super()
{
————-
————-
}
sub()
{
————–
————–
}
Here sub() function is globally available within the program from the point of its definition.


The program demonstrating function scope and file scope is as follows:

#include <iostream.h>
#include <conio.h>
void super(void)
{
cout<<”Inside super…!”;
void sub(void)
{
cout<<”Inside sub…!”;
}
cout<<”Calling sub…!”;
sub();  // sub() function accessible
//as local to the block.
}

void main()
{
cout<<”Calling Super…!”;
super();  //function invoked as defined as a
//global function and has file scope.
cout<<”Calling Sub…!”;
sub();    // sub() function not accessible as
//it is local to the function super() and
//has function scope, thus the
//function is not accessible.
getch();
}

 

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