C++ || oops concepts || INHERITANCE IN C++.

C++

C++ || oops concepts || INHERITANCE IN C++.




OOPS CONCEPT

PROPERTY OF OOPS -


  • Class
  • Object
  • Encapsulation 
  • Abstraction
  • Inheritance
  • Polymorphism
  • reusability

1)class - class is a bimding of data member or function member together is called a class
e.g - 

class student
{
int roll;
char name;
char adds;
void accept();
void display();
}s;

2)Object - object is a entity which are use to access data member and function member in a class.
e.g -

class emp
{
int id;
char name;
char dept;

void accept();
void display();

}e;

in above example e is a object.

3)encapsulation - encapsulation means hiding the information in a class is called encapsulation. it may be data member or function member.

4)Abstraction - collection of information or declaration of information is called abstraction.
i)data abstraction.
ii)function abstraction

5)Inheritance - the machanism of deriving a new class for old one is called inheritance.

e.g - Grand father ----> base class
         Father-----------> subclass/intermidiate class.
         Child ------------>subclass/derived class.

There are five types of inheritance.

i)Single inheritance -single derive from base class is called inheritance
e.g -


A is base class
B is sub class

ii)Multi level inheritance - the machanism of deriving a new class from another driving class is khown as multi level inheritance.
e.g-


iii)Multiple inheritance - several base class and one derived class is called multiple inheritance.

e.g -


A and B is a base class.
C is a derived class.

iv)Hybrid inheritance - the mixture of multiple and multi level inheritance is called hybrid inheritance.

e.g -   

v)Herarchy - one base class and several derived class is called herarchy inheritance.

e.g-




6)Polymorphism -poly means manyand morphism means forms
e.g -

                             shape draw()
             |                       |                                |
circle draw()        rectriangle()           triangle draw()

7)reusability - reusability means reuse the code and project in future.


C++ is object oriiented language. the concept of vertual function and polymorphism inheritance operator overloading concept present present in c++. and we can not called main function to other function. c-bottom up approch addopted in program design. new and delete used to allocate memory during run time. it support built in user define data type.


C++ programs
----------------------------------------------------------------------

Q1)write a program to print the table of a given number in C++.

---->

#include<iostream.h>
#include<conio.h>
void main()
{
int m,i,n;
clrscr();
count<<"Enter the value of n";
cin>>n;
for(i=1;i<=10;i++)
{
m=n*i;
cout<<m;
}
getch();
}

----------------------------------------------------------------------------

Q2)write a program in C++ to print the following pattern.

1
1 2
1 2 3

---->

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
cout<<j;
cout<<i;
cout<<*;
}
cout<<"\n";
}
getch();
}

----------------------------------------------------------------------------

Q3)write a program in C++ to print 10 different number using array.

--->

#include<iostream.h>
#include<conio.h>
void main()
{
int a[10];
int i,n;
clrscr();
count<<"Enter n \n";
cin>>n;
cout<<"Enter the element \n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
getch();
}

--------------------------------------------------------------------------

Q4)write a program in C++ to display a simple matrix.

--->

#include<iostream.h>
#include<conio.h>
void main()
{
int a[3],a[3];
int i,j,r,c;
clrscr();
cout<<"Enter r and c";
cin>>r>>c;
cout<<"Enter the element";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j];
}
cout<<"\n";
}
getch();
}

------------------------------------------------------------------------------------

Q5)write a program to find lenth of the string in C++ using inbuilt function.

--->


#include<iostream.h>
#include<conio.h>
#include<striing.h>
void main()
{
char s[10];
int n;
clrscr();
cout<<"Enter the string";
gets(s);
n=strlen(s);
cout<<"length"<<n;
getch();
}

----------------------------------------------------------------------------------------

Q6)write a program in C++ to perform the following operation using function.
1.Addition.
2.Subtraction.
3.Muilplication
4.Division

---->

#include<iostream.h>
#include<conio.h>
void main()
{
void add()
{
int a,b,c;
cout<<"enter the value of a and b";
cin>>a>>b;
c=a+b;
cout<<"addition is "<<c;
}
void sub()
{
int a,b,c;
cout<<"enter the value of a and b";
cin>>a>>b;
c=a-b;
cout<<"subtraction is "<<c;
}
void multi()
{
int a,b,c;
cout<<"enter the value of a and b";
cin>>a>>b;
c=a*b;
cout<<"multiplication is "<<c;
}
void div()
{
int a,b,c;
cout<<"enter the value of a and b";
cin>>a>>b;
c=a/b;
cout<<"division is "<<c;
}
void main()
{
clrscr();
add();
sub();
multi();
div();
getch();
}


-------------------------------------------------------------------------------------------


Q7)write the defination for a class called time that has hours,minutes and second as integer data member. the class has the following member functions: void settime(int,int,int) to set the specified values of hours. minutes and seconds in object void showtime() to display containts of time object time add(time) add the corresponding values of hours,minutes and seconds (<60) in time object argument ton current time object and make appropriate conversions and return time. time diff(time) subtract values of hours, minutes and seconds in time object argument from current time object after making appropriate conversions and return time difference write a main program to illustrate the use of above class.

---->

#include<iostream>
using namespace std;

class Time
{
private:
int hrs,min,sec;
public:
void settime(int,int,int);
void showtime();
Time add(Time);
Time diff(Time);
};

void time::settime(int a,int b,int c)
{
hrs=a;
min=b;
sec=c;
}

void Time::showtime()
{
cout<<"time is"<<hrs<<":"<<min<<":"<<sec<<end1;
}

Time Time::add(Time t1)
{
Time temp;
int i=0;
temp.sec=sec+t1.sec;
while(temp.sec>=60)
{
temp.sec-=60;
i++;
}
temp.min=min+t1.min+1;
i=0;
while(temp.min>=60)
{
temp.min-=60;
i++;
}
temp.hrs=hrs+t1.hrs+i;
return (temp);
}

Time Time::diff(Time t1)
{
Time temp;
int i=0;
temp.sec=sec-t1.sec;
while(temp.sec>=60)
{
temp.sec-=60;
i++;
}
temp.min=min-t1.min+i;
i=0;
while(temp.min>=60)
{
temp.min-=60;
i++;
}
temp.hrs=hr-t1.hrs+i;
if(temp.hrs<0)
temp.hrs*=(-1);
if(temp.min<0)
temp.min*=(-1);
if(temp.sec<0)
temp.sec*=(-1);
return (temp);
}

int main()
{
int a,b,c;
Time t,t1,t2,t3;
cout<<"Enter time 1 (hh mm ss):";
cin>>a>>b>>c;
t1.settime(a,b,c);
cout<<"Enter time 2 (hh mm ss):";
cin>>a>>b>>c;
t2.settime(a,b,c);
t=t1.add(t2);
t3=t1.diff(t2);
cout<<"time 1:";
t1.showtime();
cout<<"time 2:";
t2.showtime();
count<<"Addition of time1 and time2:";
t.showtime();
count<<"Difference of time1 and time2:";
t3.showtime();
return(0);
}

output -

Enter time 1 (hh mm ss): 5 70 90
Enter time 2 (hh mm ss): 1 30 40
Time 1: Time is 5:70:90
Time 2: Time is 1:30:40
Addition of Time1 and time2: Time is 7:42:10
Difference of Time1 and Time2: Time is 4:40:50

---------------------------------------------------------------------------------

Post a Comment

0 Comments