Object Oriented Programming Unit-1c Basic Theory

There are two simple ways in C++ to define constants −
1. Using #define preprocessor.
2. Using const keyword.

The #define Preprocessor
Following is the form to use #defines preprocessor to define a constant
#define identifier value

Following example explains it in detail:-
The const Keyword
You can use const prefix to declare constants with a specific type as follows −
const type variable = value;

Following example explains it in detail −

    Structures in C, cannot have member functions inside structures. Structures in C++ can hold member functions with member variables.

    We cannot initialize the structure data directly in C. We can directly initialize structure data in C++.

    In C, we have to write ‘struct’ keyword to declare structure type variables. In C++, we do not need to use ‘struct’ keyword for declaring variables.

    C structures cannot have static members. C++ structures can have static members.

    The sizeof operator will generate 0 for empty structure in C The sizeof operator will generate 1 for empty structure in C++

• Class is pass-by-reference and Struct is pass-by-copy, it means that, Class is a reference type and its object is created on the heap memory where as structure is a value type and its object is created on the stack memory.
• Class can create a subclass that will inherit parent's properties and methods, whereas Structure does not support the inheritance.
• A class has all members private by default. A struct is a class where members are public by default.
• Classes allow to perform cleanup (garbage collector) before object is deallocated because garbage collector works on heap memory. Objects are usually deallocated when instance is no longer referenced by other code. Structures can not be garbage collector so no efficient memory management.
• Sizeof empty class is 1 Byte where as Sizeof empty structure is 0 Bytes
• Classes are still fit for larger or complex objects and Structs are good for small, isolated model objects.

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn’t provide a value for the argument with a default value. A default argument is checked for type at the time of declaration and evaluated at the time of call.

One important point to note is that only the trailing arguments can have default values and therefore we must add defaults from right to left. We cannot provide a default value to a particular argument in the middle of an argument list.

Some illegal examples of function declaration with default values are:

int mul(int i=5, int j);

int mul(int i=0, int j, int k=10);

  • In C++, an argument to a function can be declared as const. The argument with constant value should be initialized during the function declaration.
  • Syntax
  • type function_name ( const data_type variable_name = value );

  • For example
    • #include
    • int max(const int a, int b)
    • { cout<
    • int main()
    • { max(4,3);}

The qualifier const tells the compiler that the value of that argument cannot be modified, and any such attempt will generate a compile time error. We can indirectly modify the value of the const argument by using pointer. Compile time error occurs only when we pass arguments by reference or pointer.

    In Object Oriented terms, an Object is a software unit of variables (properties) and methods (functions). An Object's method provide the only way to access the data. So that the data is hidden and secure from the accidental alteration.

    These objects are often used to model the real-world objects that you find in everyday life.

    An Object's data and methods encapsulated into a single entity. These (Data Encapsulation and Data hiding) are the key term used for describing an Object Oriented Language.

    An Object within Object Oriented Programming is an instance of a Class.

    That means a class is a blueprint for an object. Each Object was built from the same set of blueprints (Class) and therefore contains the same components.

    All Objects share the same copy of the member functions (methods), but maintain a separate copy of the member data (Properties).

    For example: Ford and Toyota share certain similar features and hence can be grouped to form the Object of the Class Car.

    Function overloading is a feature in C++ where two or more functions can have the same name but different parameters.

    Function overloading can be considered as an example of polymorphism feature in C++.

    Following is a simple C++ example to demonstrate function overloading.

One solution to this problem is to use macro definitions, commonly known as macros. Preprocessor macros are popular in C, but the major drawback with macros is that they are not really functions and therefore, the usual error checking process does not occur during compilation.
One of the major objectives of using functions in a program is to save memory space, which becomes appreciable when a function is likely to be called many times.
However, every time a function is called, it takes a lot of extra time in executing tasks such as jumping to the calling function.
When a function is small, a substantial percentage of execution time may be spent in such overheads and sometimes maybe the time taken for jumping to the calling function will be greater than the time taken to execute that function.
  • C++ has a different solution to this problem.
  • To eliminate the time of calls to small functions, C++ proposes a new function called inline function.
  • An inline function is a function that is expanded in line when it is invoked thus saving time.
  • The compiler replaces the function call with the corresponding function code that reduces the overhead of function calls.
The compiler may not perform inlining in the following circumstances:
  1. If a function contains a loop. (for, while, do-while)
  2. If a function is recursive.
  3. If a function contains static variables.
  4. If a function contains a switch command or goto statement.
When to use Inline function?

We can use Inline function as per our needs. Some useful recommendation are mentioned below-
1. We can use the inline function when performance is needed.
2. We can use the inline function over macros.
3. We prefer to use the inline keyword outside the class with the function definition to hide implementation details of the function.

A function definition in a class definition is an inline function definition, even without the use of the inline specifier

Syntax:
For an inline function, declaration and definition must be done together.
inline function-header
{
function-body
}

A member function can be called by using its name inside another member function of the same class.

This is known as nesting of member functions.
#include using namespace std;

class set
{
int x,y,z; public:
void input(void); void display(void);
int largest(int a,int b);
};

int set :: largest(int a, int b)
{
if(a >= b)
return(a);

else
return(b);
}
void set :: input(void)
{
cout << "Input value of x,y and z:"<<"\n"; cin >> x>>y>>z;
}
void set :: display(void)
{
cout << "largest value=" << largest(x,largest(y,z)) <<"\n";
}


int main()
{
set A; A.input();
A.display(); return 0;
}

  • In computer science, instantiation is the realization of a predefined object.
  • In OOP (object-oriented programming), a class of object may be defined.
  • All objects of this class have a certain set of properties (associated variables), accessories (ways to access those variables), and methods (functions).
  • An instance of that object may then be declared, giving it a unique, named identity so that it may be used in the program.
  • This process is called "instantiation".

  1. Objects communicate with one another by sending and receiving information to each other.
  2. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results.
  3. Message passing involves specifying the name of the object, the name of the function and the information to be sent.

The Object declared outside all the function bodies is known as global object.

All the functions can access the global object.

The object declared inside the functions is called local object.

The scope of the local object is limited to its current block.

  • Objects are like any variable in core blocks of memory, this memory is allocated either on the stack or on the heap.
  • Some like to call objects/vars that are allocated on the heap "dynamic".
  • But an object that is allocated on the stack basically works the same as one on the heap.
  • The only difference is that an object that is allocated on the stack has a known scope.
  • After an object allocated on the stack is no longer in scope it gets deleted and the memory that it had occupied gets freed.
  • An object allocated on the heap has an unknown scope, and in C++ the programmer must delete it manually, otherwise, you will create memory leaks.

A meta class is a class of a class i.e. the objects of this class can themselves act as classes. So a user can add or remove attributes at run time.

This whole meta thing can be summarized as – Metaclass create Classes and Classes creates objects.

The basis of the object-oriented approach is as good as possible a representation of something that exists in the real world first in a model and later in an IT system.

However, this representation will never completely correspond to reality.

Everything in the real world, whether it is a living being, an object, or an idea, is so complex and has so many aspects, that this complexity can never be completely represented.

To allow representation as a model it is necessary to focus on a few particular aspects and to leave out all others.

The essential, meaning the interesting, aspects are emphasized and all other aspects are omitted.

It is exactly this that is the art of modeling objects.

In order to model objects successfully we have to know for what purpose they are needed in the IT system.

The object “Mr. Rama” will look different in a customer management system than in a medical information system or in a tax register.

Only when we know, at least approximately, the purpose of the IT system we can build functional objects.

In models, we always abstract from reality in a target-oriented manner.

We restrict our consideration to the important aspects for the current purpose and omit everything else.

Below figure shows this step of abstraction by the example of an airplane

When depicting the real world in abstract models, we differentiate between two steps.

In the first step, we abstract from individual persons or things to objects.

In the second step, we combine similar objects into classes. Below figure shows, with a few examples, how things of the real world are depicted first as objects and then as classes