Monday, 9 April 2018

String Operations (string.h)

C language recognizes that string is a different class of array by letting us input and output the array as a unit and are terminated by null character. C library supports a large number of string handling functions that can be used to carry out many of the string manipulations such as :-

 * Length (Number of character in the string).
 * Concatenation (Adding two or more strings).
 * Comparing two strings.
 * Substring (Extract substring from a given string).
 * Copy (Copies one string over another).

To do all the operation described here it is essential to include string.h library header file in the program.

strlen( ) Function
  This function counts and returns the number of character in a string. The length does not include a null character.

 Syntax
 n=strlen(string);
                     Where n is integer variable which receives the value of length of the string.

Example :
  length = strlen("Irawen");
The function will assign number of character 9 in the string to a integer variable length.


\*Write a C program to find the length of the string using strlen( ) function. */

#include<stdio.h>
#include<string.h>
void main( )
{
char name[100];
int length;
printf("Enter the string");
gets(name);
length = strlen(name);
printf("\n Number of character in the string is=%d",length);
}

strcat( ) Function
 When you combine two strings , you add the character of one string to the end of other string. The process is called concatenation. The strcat( ) function joins two strings together. It takes the following form

Syntax :
  strcat(string1, string2)
   String1 and string2 are character arrays. When the function strcat is executed string2 is appended to string1. The string2 remains unchanged.

Example :
 strcpy(string1, "sri");
 strcpy(string2,"Bhagavan");
 printf("%s",strcat(string1,string2);
  
From the above program segment the value  of string1 becomes sribhagavan. The string at str2 remains unchanged as bhagavan.

strcmp( ) Function
  In C language we cannot directly compare the value of 2 strings in a condition like if(string1==string2).
  
Most libraries however contain the strcmp( ) function, which returns a zero if 2 strings are equal, or a non zero number if the strings are not the same. The syntax of strcmp( ) is given below :
       Strcmp(string1,string2)

 String1 and string2 may be string variable or string constants. String1 and string2 may be string variable or string constant some computers return a negative if the string1 is alphabetically less than the second and a positive number if the string is greater than the second.

Example :
strcmp("Newyork","Newyork")  will return zero because 2 strings are equal.

strcmp("their","there") will return a 9 which is the numeric difference between ASCII 'i' and ASCII 'r'.

strcmp("The","the") will return 32 which is the numeric difference between ASCII "T" & "t".


strcmpi( ) Function 
  This function is same as strcmp( ) which compares 2 string but not case sensitive.

Example :
  strcmpi("THE","the");  will return 0;

strcpy( ) Function
  C language not allow you to assign the characters to a string directly as in the statement name="Robert";

Instead use the strcpy( ) function found in most compilers the syntax of the function is illustrated below.
 strcpy(string1,string2);

Strcpy function assign the contents of string2 to string1. String2 may be a character array variable or a string constant.

strcpy(Name,"Robert");
In the above example Robert is assigned to the string called name.

strlwr( ) Funtion
 This function converts all characters in a string from uppercase to lower case.

Syntax:
 strlwr(string);

For example :
strlwr("IRAWEN") converts to Irawen.


strrev( ) Function
  This function reverses the character in a string.

Syntax
  strrev(string);

For example :
strrev("program") reverses the character in a string into "margrop".

strupr( ) Function
  This function converts all character in a string from lower case to uppercase.

Syntax
 strupr(string);

For example :
 strupr("pirawen") will convert the string to PIRAWEN.

/* Example program to use string functions */

#include<stdio.h>
#include<string.h>
void main( )
{
char s1[20], s2[20], s3[20];
int x;
printf("Enter the strings");
scanf("%s%s",s1,s2);
x=strcmp(s1,s2);
if(x!=0)
{
printf("\nStrings are not equal\n");
strcat(s1,s2);
}
else
printf("\nStrings are equal");
strcpy(s3,s1);
l1=strlen(s1);
l2=strlen(s2);
l3=strlen(s3);
printf("\n s1=%s\t length=%d characters\n",s1,l1);
printf("\n s2=%s\t length=%d characters\n",s2,l2);
printf("\n s3=%s\t length=%d characters\n",s3,l3);
}

Strings

A string is a sequence of characters. Any sequence or set of characters defined within double quotation symbols is a constant string. In C it is required to do some meaningful operations on strings they are :
* Reading string displaying strings.
* Combining or concatenating strings.
* Comparing string and checking whether they are equal.
* Extraction of a portion of a string.

Strings are stored in memory as ASCII codes of characters that make up the string appended with '\0' (ASCII value of null). Normally each character is stored in one byte, successive character are stored in successive bytes.


The last character is the null character having ASCII value zero.

Initializing Strings

  Following the discussion on character arrays, the initialization of a string must the following form which is simpler to one dimension array.
  char month1[ ] = { 'j' , 'a' , 'n' , 'u' , 'a' , 'r' , 'y'};
Then the string month is initializing to January. This is perfectly valid but C offers a special way to initialize strings. The above string can be initialized char month1 [ ]="January"; . The characters of the string are enclosed within a part of double quotes. The compiler takes care of string enclosed within a pair of a double quotes. The compiler takes care of storing the ASCII codes of characters of the string in the memory and also stores the null terminator in the end.

/* String.c string variable */
#include<stdio.h>
main( )
{
char month[15];
printf("Enter the string");
gets(month);
printf("The string entered is %s",month);
}

In this example string is stored in the character variable month. The string is displayed in the statement.
printf("The string entered is %s",month);

It is one dimension array. Each character occupies a byte. A null character(\0) that has the ASCII value 0 terminates the string. The table shows the storage of string January in the memory recall that \0 specifies a single character whose ASCII  value is zero.



Character string terminated by a null character '\0'.

A string variable is any valid C variable name and is always declared as an array. The general form of declaration of a string variable is
  Char string_name[size];
The size determines the number of characters in the string name.


Example :
  char month[10];
  char address[100];
The size of the array should be one byte more than the actual space occupied by the string since the compiler appends a null character at the end of the string.

Reading String from the Terminal
  The function scanf with %s format specification is needed to read the character string from the terminal.

Example :
 Char address[15];
 scanf("%s",address);

Scanf statement has a drawback it just terminates the statement as soon as it finds a blank space , suppose if we type the string new york then only the string "new" will be read and since there is a blank space after word "new" it will terminate the string.

Note that we can use the scanf( ) without the ampersand symbol before the variable name. In many applications it is required to process text by reading an entire line of next from the terminal.

The function getchar( ) can be used repeatedly to read a sequence of successive single characters and store it in the array.

We cannot manipulate strings since C does not provide any operators for string. For instance we cannot assign one string to another directly.

For example :
 String="xyz";
String1=string2;
Are not valid. To copy the chars in one string to another string we may do so on a character to character basis.


Writing String to Screen
  The printf statement along with format specifier %s to print strings on to the screen. The format %s can be used to display an array of characters that is terminated by the null character. For example : printf("%s",name); can be used to display the entire contents of the array name.

Arithmetic Operations on Characters
 We can also manipulate the characters as we manipulate numbers in C language. Whenever the system encounters the character data it is automatically converted into an integer value by the system. We can represent a character as an interface by using the following method.
                 X='a';
               printf("%d\n",x);
Arithmetic operations can also be performed on characters. For example x='z'-1; is a valid statement. The ASCII value of 'z' is 122 the statement therefore will assign 121 to variable x.

It is also possible to use character constant in relational expressions. For example : ch>'a' && ch <='z' will check whether the character stored in variable ch is a lower case letter.

Abstract Class

Abstract class is one that which not used to create object. An abstract class is designed only to act as base class and it is inherited by other class.

It is designed concept in the program development and provide a base upon which other classes may be built.

Remember that a class containing pure virtual function can not be used to declare the any objects of it own. As stated earlier such classes are called as abstract base class.

#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( ) {   }
virtual void putdata( )=0;
};
class B : public A
{
public:
void getdata( )
{
cout<<"\n\t This is B class getdata( )";
}
void putdata( )
{
cout<<"\n\t This is B class putdata( )";
}
};
void main( )
{
clrscr( );
A a;
B b;
b.getdata( );
b.putdata( );
getch( );
}
 

Sunday, 8 April 2018

Pure Virtual Function

It is normal practice to declare a function virtual inside the base class and redefine it in the derived classes. The function inside the base class is rarely used for performing any particular task.

It only serves as a place holder. A pure virtual function is a type of the function which has only one function declaration. It does not have the function definition. 

A pure virtual function can be declared in two different ways.
 1. A virtual function does not contain a body
      Ex virtual void getdata( )
{

}
 2. A virtual function may be equated to zero in the function declaration itself
    Ex virtual void getdata( ) = 0;

Above function are called pure virtual function. A pure virtual function is a function declared in a base class that has no definition relative to the base class. In such cases , the compiler requires each derived class to either define the function or re-declare it is a pure virtual function.

Those function which are only declared but not defined in the base class are called pure virtual function.

Program to demonstrate the pure virtual function

#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( )
virtual void putdata( )=0;
};
class B : public A
{
public:
void getdata( )
{
cout<<"\n\t This is B class getdata( )";
}
void putdata( )
{
cout<<"\n\t This is B class putdata( )";
}
};
void main( )
{
clrscr( );
A *ptr;
B b;
b.getdata( );
b.putdata( );
ptr=&b;
ptr->getdata( );
ptr->putdata( );
getch( );
}

Output:-
This is B class getdata( )
This is B class putdata( )
This is B class getdata( )
This is B class putdata( )

Containership : Classes within classes

As we know that , inheritance is the mechanism of the deriving certain or all properties of one class (base class) into another class (derived class).

Cpp also supports another way of inheriting properties of one class into another class. This approach takes a view that an object is a collection of many other objects. That is a class can contain object objects of another classes as its  members.

A class which contains the object of another class is called container class and this kind of relationship is called containership.

#include<iostream.h>
#include<conio.h>
class date
{
int dd,mm,yyyy;
public:
date( )
{
dd=0 ; mm=0 ; yyyy=0;
}
void read_date(void)
{
cout<<"(dd mm yyyy) :";
cin>>dd>>mm>>yyyy;
}
void display_date(void)
{
cout<<dd<<"\\"<<mm<<"\\"<<yyyy;
}
};
class student
{
int roll_no;
char name[20];
date dob;    //Creating object of data class
public:
void read_student(void)
{
cout<<"\nEnter roll number :";
cin>>roll_no;
cout<<"Enter name :";
cin>>name;
cout<<"Enter date of birth";
dob.read_date( );       // Calling date class function
}
void display(void)
{
cout<<"\nRoll Number :"<<roll_no;
cout<<"\nName :"<<name;
cout<<"\nDate of birth :";
dob.display_date( );
}
};
main( )
{
clrscr( );
student s;
s.read_student( );
s.display( );
getch( );
return(0);
}

Output:-
Enter roll number :101
Enter name : Irawen
Enter date of birth (dd mm yyyy) : 27 10 81
Roll Number : 101
Name : Irawen
Date of birth : 27\10\81

Virtual Destructor

A problem can occur while using polymorphism to process dynamically located object of a class hierarchy. If an object is destroyed explicitly by applying 'delete' operator to a base class pointer to the object, only the base class destructor function is called on the object.

But we are interested to delete whole memory allocated to the base class as well as derived classes. There is simple solution to this problem , declare the virtual base class destructor. This automatically makes all derived class destructor virtual even though they do not have the same name as the base class destructor.

Program to demonstrate the need of virtual destructor

#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( )
{
cout<<"\n\tThis is class A and getdata( ) method";
}
~A( )
{
cout<<"\n\tThis is a class A destructor";
}
};
class B:public A
{
public:
void getdata( )
{
cout<<"\n\tThis is class B and getdata( ) method";
}
~B( )
{
cout<<"\n\tThis is a class B destructor";
}
};
void main( )
{
clrscr( );
A *ptr;
ptr=new B;
ptr->getdata( );
delete ptr;
getch( );
}

Output:-
This is class B and getdata( ) method
This is a class A destuctor

Program to demonstrate the use of virtual destructor

#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( )
{
cout<<"\n\tThis is class A and getdata( ) method";
}
virtual ~A( )
{
cout<<"\n\tThis is a class A destructor";
}
};
class B:public A
{
public:
void getdata( )
{
cout<<"\n\tThis is class B and getdata( ) method";
}
~B( )
{
cout<<"\n\tThis is a class B destructor";
}
};
void main( )
{
clrscr( );
A *ptr;
ptr=new B;
ptr->getdata( );
delete ptr;
getch( );
}

Output:-
This is class B and getdata( ) method
This is a class B destructor
This is a class A destructor

Rules of virtual Function

1. The virtual function must be member of some class.

2. They can not be static members because virtual members is always a member of particular object in a class rather than member of class as a whole.

3. They are accessed by using object pointers.

4. A virtual function can be friend of another class.

5. A virtual function in a class must be defined even though it must not be used.

6. We can not have virtual constructor but we can have virtual destructors.

7. The keyword virtual should not be repeated in the definition if the definition occurs outside the class declaration.

8. A base class pointer can hold the address of the objects of derived classes but pointer to derived class can not hold the address of the object of base class.

#include<iostream.h>
#include<conio.h>
class base
{
public:
void display( )
{
cout<<"\nDisplay Base";
}
virtual void show( )
{
cout<<"\nShow base";
}
};
class Derived:public Base
{
public:
void display( )
{
cout<<"\nDisplay Derived";
}
void show( )
{
cout<<"\n Show derived";
}
};
void main( )
{
Base b;
Derived d;
Base *ptr;
ptr=&b;
ptr->display( );
ptr->show( );
ptr=&d;
ptr->display( );
ptr->show( );
getch( );
}

Output:-
Display Base
  Show base
Display Base
  Show derived

Polymorphism

Polymorphism is one of the crucial features of oop. It is simply means 'one name , multiple forms'. Any operation may exhibit different behaviours in different instances.

Definition:

The ability to take more than one form is known as polymorphism. An operation may exhibit different behaviors in different instances.

Ex: Consider the operation of addition , for two numbers , the operation will generate a sum. If two operands are strings then the operation would produce a third string by concatenation.

Types of polymorphism:
 1. Compiler time polymorphism or Early Binding or Static BInding
 2. Run time polymorphism or Late Binding or dynamic Binding

Compile Time Polymorphism
 1. Selecting a function in normal way during compilation time is known as compile time polymorphism or static binding.

 2. During compilation time , the cpp compiler determines which function is used , based on parameters passed to the function.

 3. Static binding is achieved by using function overloading and operator overloading.

 4. Overloading refers to the same thing for different purpose , in function overloading. We can use the same function name to perform variety of task.

#include<iostream.h>
#include<conio.h>
double volume (double l, double b, double h )
{
return(l*b*h);
}
double volume(double r, double h)
{
return(3.14*r*r*h);
}
void main( )
{
cout<<"\n\tVolume of the box is→"<<volume(1.2,2.2,3.4);
cout<<"\n\tVolume of the cylinder is→"<<volume(1.2,4.2);
getch( );
}

Output:-
Volume of the box is→8.976
Volume of the cylinder is→18.99072

Run Time Polymorphism

 . In some situation, where the function name and the prototype is the same in both the base and derived classes, since the prototype of function is the same in both the places, the function is not overloaded and therefore static binding does not apply.

2. In such cases , the appropriate member function is selected while program is running and this is known as run time polymorphism.

3. Virtual function is used to achieve run time polymorphism.

4. At run time , when it known what class objects are  under consideration the appropriate function is done dynamically at run time.

5. Dynamic binding requires the use of pointers to objects.

Destructor execution in Inheritance

Destructor in Inheritance

As we know destructor are used to free memory which is occupied by the object. A we have seen , the constructor are executed from base class to the derived class. In contrast the destructor are executed in the reverse order that is from most derived class to the base class.

#include<iostream.h>  
#include<conio.h>
 class A
 {
 public:
 ~A( )
 {
 cout<<"\n\n\t Hi I am from---> A";
}
  };
  class B:public A
  {
public:
  ~B( )
  {
  cout<<"\n\n\t Hi I am from---> B";
}
  };
  class C:public B
  {
  public:
  ~C( )
  {
  cout<<"\n\n\t Hi I am from---> C";
  }
  };
  void main( )
  {
  //clrscr( );
  C c1;  
getch( );
  }
 

Output:-
 Hi I am from---> A
 
Hi I am from---> B
 
Hi I am from---> C

Constructor execution in Inheritance

We know that constructor are used to initialize object. In the inheritance without initializing the base class members we can not derive the members of the base class to its derive class.

Therefore base class constructor are executed and then derive class constructor is executed. We must note here that if the base class constructor does not take any parameters then it is not necessary for the derived class to have a constructor.

In such situation , the base class constructor is called first and then the derived class constructor is executed.

#include<iostream.h>
#include<conio.h>
class A
{
public:
A( )
{
cout<<"\n\n\t Hi I am from A";
}
};
class B:public A
{
public:
B( )
{
cout<<"\n\n\t Hi I am from B";
}
};
class C:public B
{
public:
C( )
{
cout<<"\n\n\t Hi I am from C";
}
};
void main( )
{
clrscr( );
C c1;
getch( );
}

Output:-
Hi I am from A

Hi I am from B

Hi I am from C

Use for Heading and Paragraph

This is used for give a heading and paragraph of Website design.

Syntax:-
<h> This is Heading 1  </h>    This is a heading tag.
<p> This is a Paragraph1 </p>  This is a Paragraph tag.

Example:-
<html>

<head>

<title>Irawen </title>

</head>

<body>

<h1>Heading1</h1>

<h2>Heading2</h2>

<h3>Heading3</h3>

<h4>Heading4</h4>

<p>This is a first Paragraph</p>

<p>This is a second Paragraph</p>

<p>This is a third Paragraph</p>

<p>This is a fourth Paragraph</p>

</body>

</html>

Output:-
 

Introduction of HTML

What is HTML? 
    HTML stands for Hypertext  Markup Language , Which is used to design HTML Web pages or Templates.

Why it is called Markup Language? 
    Because it is set of markup  tags .These tags are surrounded by angle brackets.

Requirements to design HTML page?
   Browser + Text editor

How to use a HTML?

  • HTML program is always save as Filename.html
  • It is written as a notepad and notepad++.
Example:-
  <html>      //This is open tag.
  <head>
  <tittle> Welcome </tittle>     //This is write a Tittle of Website.
  </head>
  </html>  This is a closed tag.

—>This is a simple code of html.

Virtual Base Class

Consider a situation where multilevel multiple and hierarchical all the three kinds are inherited.



In the above figure the base class is inherited by both Derived1 and Derived2. Derived 3 directly inherits both Derived and Derived2.

All the public and protected member of base are inherited into Derived3 twice through both Derived 1 and Derived2.

Therefore Derived3 would have duplicate sets of members inherited. This causes ambiguity when member of base is used by Derived.

To Resolve this ambiguity, cpp includes a mechanism by which only one copy of base will be included in derived3. And this feature is called as virtual base class.

When a class is made virtual, cpp takes necessary care to inherit only only one copy of the class.

The keyword virtual precedes the base class access specifiers when it is inherited by derived class.

Ex:
Class Base
{

};
Class Derived1 : virtual public Base
{

};
Class Derived2 : virtual  public Base
{

};
Class Derived3 : public Derived1, public Derived2
{

};

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void getrollno(int n)
{
rollno=n;
}
void display1( )
{
cout<<"\n\n\tRoll no is--->"<<rollno;
}
};
class test:virtual public student
{
protected:
int cpp;
int rdbms;
public:
void getmarks(int c, int r)
{
cpp=c;
rdbms=r;
}
void display2( )
{
cout<<"\n\n\tMarks of cpp is--->"<<cpp;
cout<<"\n\n\tMarks of rdbms is--->"<<rdbms;
}
};
class spots:virtual public student
{
protected:
int sportwt;
public:
void getsportmark(int s)
{
sportwt=s;
}
void display3( )
{
cout<<"\n\n\tSport Marks is--->"<<sportwt;
}
};
class rest:public test,public sport
{
protected:
int total;
public:
void calresult( )
{
total=cpp+rdbms+sportwt;
}
void display( )
{
display1( );
display2( );
display3( );
cout<<"\n\n\tTotal Result is--->"<<total;
}
};
void main( )
{
result R;
clrscr( );
R.getrollno(101);
R.getmarks(78,87)
R.getsportmark(10);
R.calresult( );
R.display( );
getch( );
}

Output:-
Roll no is--->101
Marks of cpp is--->78
Marks of rdbms is--->87
Sport Marks is--->10
Total Result is--->175

Types of Inheritance

1. Single level Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance

Single level Inheritance
  A derived class with only one base class is called as single level inheritance.


Here 'A' is superclass or base class.From the class 'A' , the class 'B' is derived so class 'B' is called as subclass of class 'A'.
Identify the type of inheritance and implement it by writing a program for the following figure. Assume suitable member function.



#include<conio.h>
#include<iostream.h>
class student
{
int roll_no;
char name[20];
public:
void accept( )
{
cout<<"\n\tEnter Roll No of Student";
cin>>roll_no;
cout<<"\n\tEnter Name of the student";
cin>>name;
}
void display( )
{
cout<<"\n\tRollNo is--->"<<roll_no;
cout<<"\n\tName is--->"<<name;
}
};
class Eng_student : public student
{
char branch[20];
public:
void accept1( )
{
cout<<"\n\tEnter Branch of student";
cin>>branch;
}
void display( )
{
cout<<"\n\tBranch is--->"<<branch;
}
};
void main( )
{
Eng_student est;
clrscr( );
est.accept( );
est.accept1( );
est.display( );
est.display1( );
getch( );
}

Output:-
Enter Roll NO of Student 101
Enter Name of the student Irawen
Enter Branch of student CS

RollNO is--->101
Name is--->Irawen
Branch is--->CS 


Multilevel Inheritance
   
  The mechanism of deriving a new class from another "derived class" is known as multilevel Inheritance.


The above figure shows multilevel inheritance. The class A serves as a base class for the derived class B which in turn serves as a base class for the derived class C.

The class B is known as inheritance base class since it provides a link for the inheritance between A and C.

Multilevel Inheritance is declared as
Class A
{

};

Class B :public A
{

};

Class C :public B
{

};

The derived class after inheritance would contain its own data members and member function plus members from inheritance base class in addition with members from base class.


Ex:

#include<conio.h>
#include<iostream.h>
class student
{
protected:
int rollno;
public:
void getno(int a);
void putno( );
};
void student::getno(int a)
{
rollno = a;
}
void student::putno( )
{
cout<<"\n\tRollno is--->"<<rollno;
}
class test : public student
{
protected:
float sub1;
float sub2;
public:
void getmarks(float s1, float s2)
{
sub1 = s1;
sub2 = s2;
 }
void putmarks( );
{
cout<<"\n\tMarks of subject1--->"<<sub1<<"\n";
cout<<"\n\tMarks of subject2--->"<<sub2<<"\n";
}
};
class result : public test
{
float total;
public:
void display( )
{
total = sub1 + sub2;
putno( );
putmarks( );
cout<<"\n\tTotal Marks is--->"<<total<<"\n";
}
};
void main( )
{
result st1;
clrscr( );
st1.getno(101);
st1.getmarks(78,56);
st1.display( );
getch( );
}

Output:-
Rollno is--->101
Marks of subject--->78

Marks of subject2--->56

Total Marks is--->134


Multiple Inheritance

  A derived class with the several base class is class is called as multiple inheritance.



Multiple inheritance allows us to combine the features of several existing classes such as starting point for defining new classes.

Syntax:-
Class D : visibility B1, visibility B2 ................visibility Bn
{
//Body of D
};

Ex:
Class B1
{
//Body of B1;
};
Class B2
{
//Body of B2;
};
Class D : public B1 , public B2
{
 //Body of D
}; 

Write a program to implement multiple inheritance

#include<conio.h>
#include<iostream.h>
class base1
{
protected:
int m;
public:
void getm(int m1)
{
m = m1;
}
};
class base2
{
protected :
int n;
public:
void getn(int n1)
{
n = n1;
}
};
class derived:public base1,public base2
{
public:
void display( )
{
cout<<"\n\tvalue of m is--->"<<m;
cout<<"\n\tvalue of n is--->"<<n;
cout<<"\n\tvalue of (m*n) is--->"<<(m*n);
}
};
void main( )
{
derived d;
clrscr( );
d.getm(10);
d.getn(20);
d.display( );
getch( );
}

Output:-
value of m is--->10
value of n is--->20
value of (m*n) is--->200


Hierarchical Inheritance

  When one class may be inherited by more than one class class is known as hierarchical inheritance.

Programming problems can be cast into a hierarchy where certain features of one level are shared by many others below that level.



Here class 'B' and 'C' are derived from class 'A'. that is 'A' is a super class for both the class 'B' and 'C'.

Write a program to implement inheritance shown below. Assume suitable member function.



#include<conio.h>
#include<iostream.h>
class president
{
protected:
char name_p[15];
public:
void accept( )
{
cout<<"\n\tEnter Name of the president";
cin>>name_p;
}
void display( )
{
cout<<"\n\tName of the President--->"<<name_p;
}
};
class vice_president : public president
{
protected:
char name_vp[20];
public:
void accept1( )
{
cout<<"\n\tEnter Name of the vice president";
cin>>name_vp;
}
void display( )
{
cout<<"\n\tName of the vice president is--->"<<name_vp;
}
};
class secretary : public president
{
protected:
char name_s[20];
public:
void accept2( )
{
cout<<"\n\tEnter Name of the secreatry is--->"<<name_s;
}
};
void main( )
{
clrscr( );
vice_president vp;
vp.accept( );
vp.accept1( );
secretary s;
s.accept( );
vp.display( );
vp.display1( );
s.display2( );
getch( );
}

Output:-
Enter Name of the president Irawen
Enter Name of the vice president Pirawen
Enter Name of the secreatry Ntirawen

Name of the President---> Irawen
Name of the vice president--->Pirawen
Name of the secreatry is--->Ntirawen



Hybrid Inheritance

When more than one type of the inheritance is used in any design , it is called as hybrid inheritance

There could be situation where we need to apply two or more types of inheritance to design a problem.
Ex:


Consider the case of processing the students result, we have to give weightage for sports before finishing the result. Weightage for sports before finishing the result. Weightage for sports is stored on separate class called as sports.

The new inheritance relationship between various classes would be as follows.


Write a program to implement hybrid inheritance

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void getno(int a)
{
rollno = a;
}
void putno( )
{
cout<<"\n\n\tRoll No is--->"<<rollno;
}
};
class test : public student
{
protected:
float part1,part2;
public:
void getmarks(float x, float y)
{
part1 = x;
part2 = y;
}
void putmarks( )
{
cout<<"\n\n\tMarks Obtained:"<<part1<<"\t"<<part2;
}
};
class spots
{
protected:
float score;
public:
void getscore(float s)
{
score s;
}
void putscore( )
{
cout<<"\n\n\tSport Weightage is--->"<<score;
}
};
class result : public test,public sports
{
float total;
public:
void display( )
{
total = part1 + part2 + score;
putno( )
putmarks( );
putscore( );
cout<<"\n\n\tTotal Score is--->"<<total<<"\n";
}
};
void main(  )
{
result s1;
clrscr( );
s1.getno(101);
s1.getmarks(78.12,76.23)
s1.getscore(10.0)
s1.display( );
getch( );
}

Output:-
Roll NO is--->101

Marks Obtained :78.120083      76.23003

Sports Weightage is--->10

Total Score is--->164.350006

Popular Posts

Categories

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (354) Python Quiz (22) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses