Create a base class teacher with two member data name and number of students,one public function for input.one derived the class principal and publicly one data member school name. one print function both the classes and print data member of both classes.

.

coding :

#include<iostream>
using namespace std;
class teacher
{
char name[50];
int numberofstudents;
public:
void getdata()
{
cout<<"enter name";
cin>>name;
cout<<"enter no.student";
cin>>numberofstudents;
}
void display()
{
cout<<"\n name:"<<name;
cout<<"\n totalno.ofstudents"<<numberofstudents;
}
};
class principal : public teacher
{
char sname[50];
public:
void getdata()
{
     cout<<"school name";
cin>>sname;
}
void display()
{
cout<<"your school name\n"<<sname;
}
};
int main()
{
principal p;
     cout<<"enter the data\n";
p.teacher :: getdata();
p.getdata();
cout<<"display data\n";
p.display();
p.teacher :: display();
return 0;
}


output:






Definition  :        It is process of by which one class acquired the        property of another class.
  • New class is a derived class.
  • Exiting class known as base class.

🎇Types of inheritance:

  1.    Single inheritance
  2.   Multiple inheritance
  3.   Multilevel inheritance
  4.   Hierarchical inheritance
  5.   Hybrid inheritance


1) single inheritance:The derived class only one based class is  called single inheritance.


  • A=base class
  • B=derived class

Syntax of single inheritance:
                                 


2)Multiple inheritance: A derived class with several base classes called multiple inheritance.


  • A=B=base class
  • C=derived class
   syntax :




3) Hhierarchical inheritance: A properties of one class may be       inherited by more than one class is called hierarchical inheritance.


  • A=base class
  • B=C=D=derived class

   syntax:


4)Multilevel inheritance: The mechanism of deriving a class from      another derived class is known as multilevel inheritance.
   syntax:

5)Hybrid inheritance: It is a combination of all type of inheritance.

syntax:



Syntax of inheritance:

 class  derived class name :: visibility mode base class name

EX.
base class define:                         Derived class :   
class B                                               class D : visibility mode B
{                                                           {           
private:                                                   private:
       .......                                                               .........
public:                                                     public:
       ........                                                              .......
};                                                            };


  • visibility  mode is optional and it is present either private or public.
  • Default visibility mode is private.
  • Visibility mode define the features of base class privately derived or publicly derived.




all type of inheritance with example in next post..

                                🎇Recursion🎇


Definition: Recursion is a programming technique in which                                the function call itself repeatedly for some input.

Factorial function:
one can define the factorial of some number n as a product of all the integers from n to 1.

FOR EXAMPLE:

if the 5 factorial has to be calculated then,it will be=5*4*3*2*1=120

similarly 3!=3*2*1=6 and the 0!=1.
The exclamation mark is used to denote factorial.we may write the definition of factorial function as-

   n!=1 if n==0
   otherwise,n!=n*(n-1)*(n-2)*.........*1 if n>0
If the value of n is any of the 0,1,2,3 then the definition will be-
0!=1
1!=1
2!=2*1
3!=3*2*1

Here we are presenting an algorithm that takes the input as value of n and  returns the results of n!.There are two ways of doing this-

1.Iterative method     2.Recursive method

🎇Algorithm for factorial function using                  iterative definition:

prod=1;
x=n;
while(x>0)
{
prod=prod*x;
x--;
}
return(prod);

such as algorithm is called as an iterative algorithm because it calls explicit repetition of some process until certain condition is met(for example x>0)

The definition of factorial is
   n!=1 if n==0
otherwise,n!=n*(n-1)*(n-2)*....*1 if n>0

This definition is called the recursive definition of the factorial function.This definition is called recursive because again and again the same procedure of multiplication is followed but with the different input and result is again multiplied with the next input.Let us see how the recursive definition of the factorial function is used
to evaluate the 5!

step 1:5!=5*4!
step 2:          4!=4*3!
step 3:                    3!=3*2!
step 4:                              2!=2*1!
step 5:                                        1*0!
step 6:                                             0!=1.


Actually the step 6 is the only step which is giving the direct result.so to solve 5! we have to backtrack from step 6 to step 1,collecting the result from each step.Let us see how to do this.

step 6`: 0!=1
step 5`:         1!=1*0!=1
step 4`:                    2!=2*1!=2
step 3`:                              3!=3*2!=6
step 2`:                                    4!=4*3! =24      
step 1`:                                          5!=5*4!=120


Algorithm for factorial function using the Recursive definition:

if(n==0)
fact=1;
else
{
x=n-1;
y=value of x!;
fact=n*y;
}

Advantages:
1) Recursive methods bring compactness in program.
2)There is no need of using programming construct such as                 for,while,do-while.

Disadvantages:
1)Memory utilization is more in recursive functions because all           pending operations must be preserved.
2)Recursive methods are less efficient than iterative methods.
3)Recursive methods are complex to implement.

Properties of Recursive definition:

The very essential property of recursive definition is that there should be atleast one non recursive call,because of which the procedure won`t go in the infinite condition.Thus the non recursive
exit will help a recursive procedure to terminate.

FOR EXAMPLE:

if(n==0)
  return 1;
is a non recursive 
call for factorial


Another property of recursive function is that any instant of recursive definition must eventually reduce to some manipulation.By this one can reach to the answer after certain number of steps.


🎇program for finding out the factorial for any given number.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main(void)
{
int n,f;
int fact(int n);
clrscr();
printf("\n program for finding factorial number");
printf("\n enter the number for finding the factorial");
scanf("%d",&n);
f=fact(n);
        printf("\n The factorial of %d is %d",n,f);
getch();
}
int fact(int n)
{
int x,y;
if(n<0)
{
printf("the negative parameter in the factorial function");
exit(0);
}
if(n==0)
{
return 1;
x=n-1;
y=fact(x);
return (n*y);
}
}


output:
program for finding the factorial number
enter the number for finding the factorial=5
The factorial of 5 is 120
Representation of stack using array:

Declaration 1:

#define size 100
int stack[size], top=-1;

In the above declaration stack is nothing but an array of integers,And most index of that array will act as a top.


The stack is of the size 100.As we insert the numbers,the top will get incerement .the elements will be placed from 0th position in the stack.At the most we can store elements in the stack,so at the most last element can be at (size -1) position,index 99.

Declaration 2:

#define size 10
struct stack
{
  int s[size];
  int top;
}st;

above declaration stack is declared as a structure.


Now compare declaration 1 and 2.both are of stack declaration only.But the second declaration will always preferred.WHY?
Because in the second declaration we have used a structure for stack elements and top,By this we are binding or co-relating top variable with stack elements.Thus top and stack are associated with each other by putting them together in a structure.The stack can be passed to the function by simply passing the structure variable.

We will make use of the second method of representing the stack in our program.

  • The stack can also be used in the database.For example if we want to store marks of all the students of fourth semester we can declare a structure of stack as follows:



Thus we can store the data about whole class in our stack.The above declaration means creation of stack. Hence we will write only push and pop function to implement the stack.And pushing or popping we should check whether stack is empty or full.

key point= IF top = MAXSIZE means stack is full.

stack empty operation

Initially  stack is empty.At that time the top should be initialized to -1 or 0.If we set top to -1 initially then the stack will contain the elements from 0th position and if we set top to 0 initially the elements will be stored from 1st position,in the stack.
Elements may be pushed onto the stack and there may be a case that will the elements are removed from the stack.Then the stack becomes empty.thus whenever top reaches to -1 we can say the stack is empty.

int stempty()
{
 if(st.top==-1)
return 1;
else
return 0;
}

stack full operation

In the representation of stack using array means size of stack.As we go on inserting the elements the stack gets filled with the elements.
So it is necessary before inserting the elements to check whether the stack is full or not.stack full condition is achieved when stack reaches to maximum size of array.

int stfull()
{
if(st.top>=size-1)
return 1;
else
return 0;
return 0;
}
 push operation:

push is function which inserts new elements at the top of the stack.

void push()
{
st.top++;
st.s[st.top]=item;
}



pop operation:

 which deletes the elements at the top of the stack.

 int pop()
{
int item;
item=st.s[st.top];
st.top--;
return[item];
}






Time complexity 
Definition : Amount of time required by an algorithm                         to execute is called time complexity.
Time complexity is the amount of time taken by the program for execution.As it is difficult to measure the time complexity in terms of clock units,we will measure the time complexity using the frequency count.

FREQUENCY COUNT: The efficiency of a program is measured by inserting a counter in the algorithm in order to count the number of times the basic operation is executed. this is a straightforward method of measuring the time complexity of the program. 

EX.
1)consider following piece of the code for obtaining the frequency count-

void display()
{
    int a,b,c;
    a=10;
    b=20;
    c=a+b;
    printf("%d",c);
}

solution:




2)find the frequency count of:

i) for(i=1;i<=n;i++)
   for(j=1;j<=i;j++)
    x=x+1;

solution:




Space complexity

Definition: Amount of storage required by an                       algorithm is called space complexity.


space complexity can be defined as  amount of memory required by an algorithm.

It compute the space complexity we use two factors: constant and instance

                                      s(p)=C+s(p)

c is a constant.fixed part and it denotes the space of input and outputs.this is an amount of space taken by instruction,variable and identifiers.And sp is a space dependent upon instance characteristics.This is a variable part whose space requirement depends on particular problem instance.

fixed part includes space for:
  • instructions
  • variable
  • Array size
  • Space for constant

variable part includes space for:

  • Recursion stack for handling recursive call.
  • The variable whose size is dependent upon the particular problem instance being solved. The control statements (such as for,do,while,choice) are used to solve such instance.

EX.

compute the space complexity for the code fragment:

Algorithm sum(a,n)
{
   s:0.0; 
     for i: 1 to n do
          s: s + a[i];  
            return s;
}

solution:

given code we require space for:
   
s: =0   require 1 unit of space
for i:  to n   require n unit of space
s: s + a[i];   require n units of space
returns;   require  units of space


thus total 2n+2 units of space is required.if we neglect the constants of this equation and if consider the order of magnitude then the space complexity is denoted using Big Oh notation as O(n).


Create two classes DM and DB which store the value of distances. DM stores distances in meters and centimeters and DB in feet and inches. Write a program that can read values for the class objects and add one object of DM with another object  of DB. Use a friend function to carry out the addition operation. The object that stores the results  may be a DM object or DB object, depending on the units in which the results are required. The display should be in the format of feet and inches or meters and centimeters depending on  the object on display.
1 Feet = 0.3048 Meter & 1 Meter = 3.28 Feet  
1 Inch = 2.54 Centimeter & 1 Centimeter = 0.3937 Inch

Use friend function: Use friend function is limited purpose,friendship is not mutual that means is class A friend of class B then class B does not become friend of class A automatically.

  • Friend function can be given special access to the private and protected member.
  • Friendship is not inherited.
  • Friend class :All member function of all class as friend function of another class. In such cases the class is called friend class.
  • In this friend function,one class of data member access on another class in private part then and then use friend function OR if we use of data member of first class to another class in member function then use friend function.
  • function is keyword.

Friend function syntax:
 
friend return_type class_name :: function();

what use of operator+:

overloaded operator are redefine in c++ class using keyword of operator follow by operator symbol.

operator function should be either member function or friend function.
friend function required one argument for unary operator and two argument fro binary operator.

It is essentially pass to object by value or reference.

process:


1 Feet = 0.3048 Meter & 1 Meter = 3.28 Feet  
1 Inch = 2.54 Centimeter & 1 Centimeter = 0.3937 Inch

in DB operator+ function ,
convert meter into cm and feet into inches and after summation of feet and inches with convert cm into inches.
   

coding:

#include<iostream>
using namespace std;
 class DB;
class DM
{
float meter,cm;
public:
DM()
{
}
DM(float a,float b)
{
meter=a;
cm=b;
}
friend DB operator+(DM,DB);
};
class DB
{
float feet,inches;
public:
DB()
{
}
DB(int x,int y)
{
feet=x;
inches=y;
}
void display()
{
cout<<"\n feet is:"<<feet;
cout<<"\n inches is:"<<inches;
}
friend DB operator+(DM,DB);
};
DB operator+(DM e,DB f)
{
DB ss;
e.cm+=e.meter*100;
f.inches+=f.feet*12;
ss.inches=e.cm*0.3937+f.inches;
if(ss.inches>=12)
{
ss.feet++;
ss.inches-=12;
}
return ss;
}
int main()
{
DM d1(10,20);
DB d2(12,13);
DB sd;
sd=d1+d2;
sd.display();
return 0;
}


output:



Create a class that imitates part of the functionality of the basic data type int . Call the class Int (note different capitalization). The only data in this class is an int variable. Include member functions to initialize an Int to 0, to initialize it to an int value, to display it (it looks just like an int ), and to add two Int values. Write a program that exercises this class by creating one uninitialized and two initialized Int values, adding the two initialized values and placing the response in the uninitialized value, and then displaying this result.

Understanding Things:





coding:

#include<iostream>
using namespace std;
class Int
{
int num,num1;
public:
Int()
{
}
Int(int n)
{
num=n;
}
int add(Int n1,Int n2)
{
num1=n1.num+n2.num;
return num1;
}
};
int main()
{
int sum;
Int n1(2),n2(3),n3;
sum=n3.add(n1,n2);
cout<<"sum of two number is:"<<sum;
return 0;
}

output:


Copyright © 2013 free coding