Posts

Showing posts with the label Object Oriented Programming

Difference Between Procedure Oriented Programming (POP) & Object Oriented Programming (OOP)

Difference Between Procedure Oriented Programming (POP) & Object-Oriented Programming (OOP) | C++ Programming Procedure Oriented Programming Object-Oriented Programming Divided Into In POP, the program is divided into small parts called  functions . In OOP, the program is divided into parts called  objects . Importance In POP, Importance is not given to  data  but to functions as well as the  sequence  of actions to be done. In OOP, Importance is given to the data rather than procedures or functions because it works as a  real world . Approach POP follows  Top-Down approach . OOP follows  Bottom-Up approach . Access Specifiers POP does not have any access specifier. OOP has access specifiers named Public, Private, Protected, etc. Data Moving In POP, Data can move freely from function to function in the system. In OOP, objects can move and communicate with each other through member functions. Expansion To add new data and function in POP is not so easy. OOP provides an

C++ Program For Banking System Using Class | C++ Programming

C++ Program For Banking System Using Class #include <iostream> using namespace std ; #include <iomanip> class bank {     char name [ 20 ];     int acno ;     char actype [ 20 ];     int bal ; public:     void opbal ( void );     void deposit ( void );     void withdraw ( void );     void display ( void ); }; void bank :: opbal ( void ) {     cout << endl          << endl;     cout << "Enter Name :-" ;     cin >> name ;     cout << "Enter A/c no. :-" ;     cin >> acno ;     cout << "Enter A/c Type :-" ;     cin >> actype ;     cout << "Enter Opening Balance:-" ;     cin >> bal ; } void bank :: deposit ( void ) {     cout << "Enter Deposit amount :-" ;     int deposit = 0 ;     cin >> deposit ;     cout << " \n Deposit Balance = " << bal + deposit ; } void bank :: withdraw ( void ) {     int withdraw ;     c

Access Modifiers in C++

Access Modifiers in C++ Access modifiers are constructs that define the scope and visibility of members of a class. There are three access modifiers:

Types of Inheritance in C++ | C++ Programming

Types of Inheritance A class can inherit properties from one or more classes and from one or more levels. On the basis of this concept, there are five types of inheritance. Single Inheritance Multiple Inheritance Hierarchical Inheritance Multilevel Inheritance Hybrid Inheritance Single Inheritance In single inheritance, a class is derived from only one base class. The example and figure below show this inheritance. Example class A {  members of A }; class B  :  public A {  members of B }; Multiple Inheritance In this inheritance, a class is derived from more than one base class. The example and figure below show this inheritance. Implementation Skeleton: class A { members of A }; class B {  members of B }; class C  : public A, public B  { members of C }; Hierarchical Inheritance In this type, two or more classes inherit the properties of one base class. The example and figure below show this inheritance. Implementation Skeleton: class A { member

Type Conversion In C++

Type Conversion The type conversions are automatic as long as the data types involved are built-in types. If the data types are user-defined, the compiler does not support automatic type conversion and therefore, we must design the conversion routines by ourselves. Three types of situations might arise in the data conversion in this case. 1. Conversion from basic type to class type 2. Conversion from class type to basic type 3. Conversion from one class type to another class type

Namespace in C++

Namespace A program includes many identifiers defined in different scopes. Sometimes an identifier of one scope will overlap (i.e. collide) with an identifier of the same name in a different scope, potentially creating a problem. Identifier overlapping also occurs frequently in third-party libraries that happen to use the same names for global identifiers (such as functions).

Objects as Function Arguments in C++ Programming | C++ Programming

Objects as Function Arguments Like any other data type, an object may be used as a function argument in three ways: pass-by-value, pass-by-reference, and pass-by-pointer.

Class and Object and Memory in C++ Programming | C++ Programming

Image
Class and Object and Memory in C++ Programming   When a class is specified, memory space of all the member function is allocated but memory allocation is not done for the data member.   When an object is created, memory allocation for its data members is done. The logic behind separate memory allocation for member functions is quite obvious. All instances of a particular class would be using the same member functions but they may be storing different data in their data members.   Memory allocation for objects is illustrated in fig below Class and Object and Memory in C++ Programming It can be observed that “n” objects of the same class are created and data members of those objects are stored in distinct memory location, whereas the member functions of object 1 to object n are stored in the same memory area. Therefore, each object has a separate copy of data members and the different objects share the member functions among them.

Function overloading in C++ | C++ Programming

Function overloading in C++ Programming Overloading refers to the use of the same thing for a different purpose. When the same  function name is used for different tasks this is known as function overloading. Function overloading is one of the important features of C++ and any other OO languages. When an overloaded function is called the function with matching arguments and return type is invoked. e.g. void border(); //function with no arguments void border(int ); // function with one int argument void border(float); //function with one float argument void border(int, float);// function with one int and one float arguments  For overloading a function prototype for each and the definition for each function that share same name is compulsory. //function overloading //multiple function with same name #include&lt;iostream.h&gt; #include&lt;conio.h&gt; int max(int ,int); long max(long, long); float max(float,float); char max(char,char); void main() {   re

Passing and Returning by reference | C++ Programming

Passing by references in C++ Programming          We can pass parameters in function in C++ by reference. When we pass arguments by reference, the   formal arguments in the called function become aliases to the   actual arguments in the calling   function i.e. when the function is working with its own arguments, it is actually working on the original data.

Default Arguments and Constant Arguments

Default Arguments In C++ a function can be called without specifying all its arguments. In such cases, the function assigns a default value to the parameter which does not have a matching argument in the function call. The default value is specified when function is declared.  

Reference Variables | C++ Programming

Reference Variables:               C++ Programming introduces a new kind of variable known as reference variable. A reference variable provides an alias (Alternative name) of the variable that is previously defined. For example, if we make the variable sum a reference to the variable total, the sum and total can be used interchangeably to represent that variable.   Syntax for defining reference variable Data_type & reference_name = variable_nane   Example: int total=100 ; int &sum=total;                Here total is int variable already declared. Sum is the alias for variable total. Both the variable refer to the same data 100 in the memory.   cout< <total;    and  cout<<sum; gives the same output 100.   And total= total+100;   Cout< <sum;     //gives output 200                   A reference variable must be initialized at the time of declaration. This establishes the correspondence between the reference and the data object which it means. The initi

Scope Resolution Operator( :: ) | C++ Programming

Scope Resolution Operator( :: ) | C++ Programming C++ programming supports a mechanism to access a global variable from a function in which a local variable is defined with the same name as a global variable. It is achieved using the scope resolution operator ::  

Advantages and Disadvantages of OOP | C++ Programming

Advantages and Disadvantages of OOP Advantages of OOPs Object oriented programming contributes greater programmer productivity, better quality of software and lesser maintenance cost.  The main advantages are: Making the use of inheritance, redundant code is eliminated, and the existing class is extended. Through data hiding, programmer can build secure programs that cannot be invaded by code in other parts of the program. It is possible to have multiple instances of an object to co-exist without any interference. System can be easily upgraded from small to large systems. Software complexity can be easily managed. Message passing technique for communication between objects makes the interface description with external system much simpler. Aids trapping in an existing pattern of human thought into programming. Code reusability is much easier than conventional programming languages. Disadvantages of OOPs Compiler and runtime overhead. Object oriented progr

Object Oriented Approach | C++ Programming

Image
Object Oriented Approach | C++ Programming                 The fundamental idea behind object-oriented language is to combine both data & function that separate data into a single unit such a unit is called object. A objects function called member function provides the way to access objects data.

Procedural Oriented Approach | C++ Programming

Image
Procedural Oriented Approach | C++ Programming In procedural oriented approach programs are organized in form of subroutines. The subroutines do not let the code duplication. P.O. is suitable for medium sized software C, Pascal, FORTRAN etc. Are procedural oriented language.                

Function Templates and Class Templates in C++ Programming | C++ Programming

Templates in C++ Programming  Function templates in C++ Programming  Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type. The format for declaring function templates with type parameters is: template  function_declaration; template  function_declaration; The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have

Inheritance in C++ Programming | C++ Programming

Inheritance in C++ Programming  Introduction Inheritance (or derivation) is the process of creating new classes, called derived classes, from existing classes, called base classes. The derived class inherits all the properties from the base class and can add its own properties as well. The inherited properties may be hidden (if private in the base class) or visible (if public or protected in the base class) in the derived class.

Inline function in C++ Programming | C++ Programming

Inline function in C++ Programming A inline function is a short-code function   written and placed before main function   and compiled as inline code. The prototyping is not required for inline function. It starts with keyword inline . In ordinary functions, when function is invoked the control is passed to the calling function and after executing the function the control is passed back to the calling program.           But , when inline function is called, the inline code of the function is inserted at the place of call and compiled with the other source code together. That is the main   feature of inline function and different from the ordinary function.   So using inline function executing time is reduced because there is no transfer and return back to control. But if function has long code inline function is not suitable because it increases the length of source code due to inline compilation. // Inline Function //saves memory, the call to function cause the same co

Function in C++ Programming | C++ Programming

Default Arguments When declaring a function, we can specify a default value for each of the last parameters which are called default arguments. This value will be used if the corresponding argument is left blank when calling the function. To do that, we simply have to use the assignment operator and a value for the arguments in the function declaration. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is ignored and the passed value is used instead.  For example: // default values in functions using namespace std ; int divide ( int a , int b = 2 ) {     int r ;     r = a / b ;     return ( r ); } int main () {     cout << divide ( 12 );     cout << endl;     cout << divide ( 20 , 4 );     return 0 ; } As we can see in the body of the program there are two calls to function divide. In the first one: divide (12) We have only specified one argument, but the