Showing posts with label OOCP. Show all posts
Showing posts with label OOCP. Show all posts

Monday, March 4, 2013

OOCP (CPP) : Example # 15 : CPP Inheritance Parameterised constructor

OOCP (CPP)  :  Example #  15 : CPP Inheritance Parametrized constructor

* Lets start learning about cpp inheritance.
* Following example is simple inheritance : Single Inheritance consist of one base class and one derived class.
* This example will demonstrate about how to pass parameter to constructor while using inheritance

//Written by Latest Technology Guide    
//Title : OOCP (CPP)  :  Example #  
15 : CPP Inheritance Parameterised constructor

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

class B
{
private:
int a;
protected:
void printinfo();
static int i; //Static member must be protected or public
public:
// B(int x=0) : a(x) {} //Short method to initialize
// Or
B(int x=0)
{
a = x;
cout << "Base class constructor called..." << ++i << endl;
cout << "Value of A : " << a << endl;
}
~B()
{
cout << "Base class Destructor called..." << --i << endl;
}
void print()
{
B :: printinfo();
}
};

int B :: i =0;

void B :: printinfo()
{
cout << "A ==> " << a;
cout << " , Size of Base class B is : " << sizeof(this->a) << endl;
}

class D : public B
{
private:
int b;
public:
D(int y=0) : B(y), b(y+10)
// Or
// D(int y=0)
{
// B(y);
cout << "Derived class construtor called..." << ++i << endl;
// b = y+10;

}
~D()
{
cout << "Derived class destructor called... " << --i << endl;
}
void print();
};
void D :: print()
{
B :: printinfo();
cout << "B ==> " <<  b;
cout << " , Size of Derived class D is : " << sizeof(this->b) << endl;
}

//=================================================
// Void Main
//=================================================
void main()
{
clrscr();
B b(100); //With parameter initialize by 100 : base class only
B b1; //Without parameter by 0
D d(200); //Base class obj construct first then derived: a=200 b=210
D d1; //a = 0  b=10
cout << endl << endl;
b.print();
d.print();
cout << endl << endl;
cout << "Sizeof Base    class is : " << sizeof(b) << endl;
cout << "Sizeof Derived class is : " << sizeof(d) << endl;

getch();
}


Output:
* You will find output as you have entered value.

--------------------------------------------------------------------------------
OOCP (CPP)  :  Example #  15 : CPP Inheritance Parameterised constructor

* Example demonstrate how to create your first inheritance program using cpp example 2.
* in example B stands for Base Class while D stands for Derived class.


Note: 
All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements. 

Saturday, March 2, 2013

OOCP (CPP) : Example # 14 : CPP Single Inheritance Example 2


OOCP (CPP)  :  Example #  14 : CPP Single Inheritance Example 2

* Lets start learning about cpp inheritance.
* Following example is simple inheritance : Single Inheritance consist of one base class and one derived class.


//Written by Latest Technology Guide    
//Title : OOCP (CPP)  :  Example #  
14 : CPP Single Inheritance Example 2


#include <iostream.h>


#include <conio.h>

class B

{
protected:
int a;
public:
int b;
B()
{
cout << "Base class constructor called..." << endl;
}
void get_ab();
void line()
{
cout << "Base class Line function called..." << endl;
}
~B()
{
cout << "Base class Destructor called..." << endl;
}
};

class D : public B

{
private:
int c;
public:
D()
{
cout << "Derived class construtor called..." << endl;
}
void mul();
void display();
void line()
{
cout << "Derived class line function called..." << endl;
}
~D()
{
cout << "Derived class destructor called... " << endl;
}

};


void B :: get_ab()

{
cout << "Enter the value of A and B " << endl;
cin >> a >> b;
}

void D :: mul()

{
get_ab();
c = b * a;
}
void D :: display()
{
cout << "Derived class called " << endl;
cout << " A ==> " << a << endl;
cout << " B ==> " << b << endl;
cout << " C ==> " << c << endl;
}

//=================================================

// Void Main
//=================================================
void main()
{
clrscr();
D D1; //derived class object

D1.get_ab();

D1.mul();
D1.display();

cout << endl;


D1.b = 20;

D1.mul();
D1.display();
D1.line();

getch();

}

Output:
* You will find output as you have entered value.

--------------------------------------------------------------------------------
OOCP (CPP)  :  Example #  14 : CPP Single Inheritance Example 2

* Example demonstrate how to create your first inheritance program using cpp example 2.
* in example B stands for Base Class while D stands for Derived class.


Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements. 

Thursday, February 28, 2013

OOCP (CPP) : Example # 13 : CPP Single Inheritance

OOCP (CPP)  :  Example #  13 : CPP Single Inheritance 

* Lets start learning about cpp inheritance.
* Following example is simple inheritance : Single Inheritance consist of one base class and one derived class.


//Written by Latest Technology Guide    
//Title : OOCP (CPP)  :  Example #  
13 : CPP Single Inheritance 

// Inheritance 01 - Simple Derived class as private from Base class
// Single Inheritance

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

class B
{
private:
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
void line()
{
cout << "Base class Line function called..." << endl;
}
};

class D : private B
{
private:
int c;
public:
void mul();
void display();
void line()
{
cout << "Derived class line function called..." << endl;
}

};

void B :: get_ab()
{
cout << "Enter the value of A and B " << endl;
cin >> a >> b;
}

int B :: get_a()
{
return a;
}

void B :: show_a()
{
cout << "Base class called..." << endl;
cout << " A ==> " << a << endl;
}
void D :: mul()
{
get_ab();
c = b * get_a();
}
void D :: display()
{
cout << "Derived class called " << endl;
show_a();
cout << " B ==> " << b << endl;
cout << " C ==> " << c << endl;
}

//=================================================
// Void Main
//=================================================
void main()
{
clrscr();
D D1; //derived class object

D1.mul();
D1.display();

cout << endl;

D1.mul();
D1.display();
D1.line();

getch();
}


Output:
* You will find output as you have entered value.

--------------------------------------------------------------------------------
OOCP (CPP)  :  Example #  13 : CPP Single Inheritance 

* Example demonstrate how to create your first inheritance program using cpp.
* in example B stands for Base Class while D stands for Derived class.


Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements. 

Wednesday, February 27, 2013

OOCP (CPP) : Example # 12 : CPP Operator New and Delete

OOCP (CPP)  :  Example #  12 : CPP Operator New and Delete.

* Lets allocate memory run time.
* new operator example using cpp language.

* delete operator example using cpp language.
//Written by Latest Technology Guide    
//Title : OOCP (CPP)  :  Example # 
 12 : CPP Operator New and Delete.


#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

class String
{
private:
char *name;
int length;
public:
String(char *s)
{
length=strlen(s);
name = new char[length+1];
strcpy(name,s);
}
String()
{
length=0;
name = new char[length+1];
}
void display()
{
cout << "Name : " << name << endl;
}
void join(String &a, String &b);
};

//=================================================
// Join Member Function of String
//=================================================
void String :: join(String &a, String &b)
{
length = a.length + b.length;
delete name;

name = new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
}

//=================================================
// Main Function
//=================================================
void main()
{
clrscr();
String n1("MCA "),n2(" GTU "),n3("India"),s1,s2;

//main purpose of join
s1.join(n1,n2);
s2.join(s1,n3);

n1.display();
n2.display();
n3.display();
s1.display();
s2.display();

getch();
}

Output:
* You will find output as combination of string1 and string2.

--------------------------------------------------------------------------------
OOCP (CPP)  :  Example #  12 : CPP Operator New and Delete.

* Example demonstrate how to allocate memory on the go (Runtime memory allocation using cpp).
* Program demonstrate use of new operator in cpp.

Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements. 

Sunday, February 24, 2013

OOCP (CPP) : Example # 11 : Concatenate two string using class method join in C++



OOCP (CPP)  :  Example #  11 : Concatenate two string using class method join in C++

* Lets start learning CPP (C++) Using Example.
* Constructor Example using c++.

//Written by Latest Technology Guide    
//Title : OOCP (CPP)  :  Example # 
 11 : Concatenate two string using class method join in C++

#include <iostream.h>

#include <iomanip.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>

class String
{
private:
char *st;
int len;
public:
String()
{
cout << endl << "Simple Constructor called..." << endl;
}
String(char *s)
{
cout << endl << "1 Para Constructor called..." << endl;
strcpy(st,s);
len = strlen(st);
}
void join(String a, String b);
void display();
};

void String :: join(String a, String b)
{
strcat(a.st,b.st);
strcat(st,a.st);
len = strlen(st);
}
void String ::  display()
{
cout << "String is : " << st << endl;
cout << "Lengt  is : " << len << endl;
}


//=================================================
// Void Main
//=================================================

void main()
{
String S1("Abcd "),S2("Xyz "),S3("Pqr ");

cout << "String 1 is : " << endl;
S1.display();
cout << "String 2 is : " << endl;
S2.display();
cout << "String 3 is : " << endl;
S3.display();
cout << "S1.Join(2,3) is : " << endl;

S1.join(S2,S3);
S1.display();
getch();
}


Output:
* You will find output as combination of string1 and string2.

--------------------------------------------------------------------------------
OOCP (CPP)  :  Example #  11 : Concatenate two string using class method join in C++

* Example demonstrate how to concatenate two string using method in c++.
* String class contains two constructor and having two method named join and display.
* Join method of string class can be used to join two string.
* Display method can be used to display value from the private variable.

Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements. 

Saturday, February 23, 2013

OOCP (CPP) : Example # 10 : Inline Function Example using c++



OOCP (CPP)  :  Example #  10 : Inline Function Example using c++

* Lets start learning CPP (C++) Using Example.
* Constructor Example using c++.

//Written by Latest Technology Guide    
//Title : OOCP (CPP)  :  Example # 
 10 : Inline Function Example using c++


/*
Inline 01

If function is called following activities carried out
- Jumping to the function
- Saving the register
-    Pushing the argument into stack.
- Return to the calling function

These activities can be avoided if we use inline....

Inline functin cannot work in following situation
- For function returning a value using If, Loop, Switch, goto
- For function not returning value and return statements are exist
- If function contains static variable
- if inline function is recursion
*/

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


class Digits
{
private:
long int n;
public:
Digits(long int pass)
{
n = pass;
}
inline int sum(long int m, int add);
void display(long int);
~Digits()
{
cout << "Destructor is called " << endl;
}
};

//=================================================
// Inline member function
//=================================================
int inline Digits :: sum(long int m, int add)
{
return(add + m%10);
}

//=================================================
//  Display member function
//=================================================
void Digits :: display(long int m)
{
cout << "Sum is : " << m << endl;
}

//=================================================
// Main Function
//=================================================
void main()
{
clrscr();
long int n;
int temp = 0;

cout << "Enter the long int no ";
cin >> n;

Digits d1(n);

while(n>0 || temp>9)
{
temp = d1.sum(n,temp);
n = n / 10;
d1.display(temp);

if(n==0 && temp > 9)
{
n = temp;
temp = 0;
}
} //while

getch();
} //void main


Output:
* Inline Function Example using c++.

--------------------------------------------------------------------------------
OOCP (CPP)  :  Example #  10 : Inline Function Example using c++

* Example demonstrate how to set function as inline function in c++.



Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements.