Sunday, September 25, 2016

TRIANGLE NUMBERS :)

This C++ Program Displays First 'n' Triangle Numbers.

The Sequence of Triangle Numbers are Generated by Adding Natural Numbers.
So the 5th Triangle Number Would be 1+2+3+4+5=15.

#include <iostream>
using namespace std;
int main()
{
int i,j,n,ans=1;
cout<<"Enter value of n: ";
cin>>n;
cout<<"\nFirst "<<n<<" Triangle Numbers are:\n";
cout<<"1\n";
for(i=2;i<=n;i++)
{
ans+=i;
cout<<ans<<"\n";
}
return 0;
}



PASCAL TRIANGLE:

C++ Program to Display Pascal Triangle.

#include <iostream>
using namespace std;
int main()
{
int a[20][20],i=0,j=0,n;
cout<<"Enter Size of Pascal Triangle: ";
cin>>n;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
a[i][j]=0;
}

a[0][0]=1;  //Making First and Second Rows as 1's
a[1][0]=1;
a[1][1]=1;

for(i=2;i<n;i++)
{
a[i][0]=1;
for(j=1;j<n;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}

cout<<"\nPascal Triangle is:\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
cout<<a[i][j];
cout<<"\t";
}
cout<<endl;
}
return 0;
}


Transforming INFIX to POSTFIX Expression

Program to Convert An Infix Expression To Postfix Expression:


#include<stdio.h>
#include<ctype.h> //for isalnum function
char stack[50];
int top=-1;
void push(char x)
{
if(top==49)
{printf("Overflow");
}
else{

stack[++top]=x;
}
}
char pop()
{ if(top==-1)
{
return -1;
}
else{

return stack[top--];
}
}
int priority(char x)
{
if(x=='(')
return 0;
else if(x=='+'||x=='-')
 return 1;
 else if(x=='*'||x=='/')
 return 2;
}
main()
{
char infix[50];
char x,*e;  //pointer to access each character
printf("Enter The Expression  ");
scanf("%s",infix);
e=infix;
while(*e!='\0')
{
if(*e=='(')
{
push(*e);
}
else if(isalnum(*e))
{
printf("%c",*e);
}
else if(*e==')')
{
while((x=pop())!='(')
printf("%c",x);
}
else
{
while(priority(stack[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e++;}
while(top!=-1)
printf("%c",pop());
}

Output:


Hybrid Inheritance

Hybrid Inheritance is that type in which we combine two or more types of inheritance.

Program To Illustrate the Concept of Hybrid Inheritance:

#include<iostream>
using namespace std;
class Student
{
int rollno;
public:
void getnum(int a)
{
rollno=a;
}
void putnum()
{
cout<<"Roll Number is "<<rollno;
}
};
class Marks:public Student
{ protected:
float sub1,sub2;
public:
void getmarks(float a,float b)
{
sub1=a;
sub2=b;
}
void display()
{
cout<<"\nSubject 1="<<sub1;
cout<<"\nSubject 2="<<sub2;
}

};
class sports
{protected:
int sport;
public:
void getsport(int a)
{
sport=a;
}
void displaysport()
{
cout<<"\nMarks In Sports="<<sport;
}
};
class result:public sports,public Marks
{
float total;
public:
void output()
{total=sub1+sub2+sport;
cout<<"\nTotal Marks="<<total;
}
};
main()
{
result r;
r.getnum(712299);
r.getmarks(98.5,89.6);
r.getsport(89);
r.putnum();
r.display();
r.displaysport();
r.output();
}

Also See Hierarchical Inheritance



Hierarchial Inheritance

Program to Illustrate The Concept Of Hierarchical Inheritance:


#include<iostream>
using namespace std;
class Number
{
int num;
public:
void getnum()
{
cout<<"\nEnter any Number ";
cin>>num;
}
int returnum()
{
return num;
}
};
class Square:public Number
{
int sqr;
public:
void square()
{
int num=returnum();
sqr=num*num;
cout<<"Square of "<<num<<" is "<<sqr;
}
};
class Cube:public Number
{
int cube;
public:
void cuube()
{
int num=returnum();
cube=num*num*num;
cout<<"Cube of "<<num<<" is "<<cube;
}
};
main()
{
Square a;
a.getnum();
a.square();
Cube b;
b.getnum();
b.cuube();
}
Also See Multiple Inheritance

Saturday, September 24, 2016

Multiple Inheritance

Program To Illustrate The Concept Of Multiple Inheritance:


#include<iostream>
using namespace std;
class base1
{
    int a;
    protected:
        int b;
    public:
        int c;
        void getdata()
        {
            cout<<"Enter The Values Of A,B,C\n";
            cin>>a>>b>>c;
        }
        void putdata()
        {
            cout<<"Values Are\n";
            cout<<"\nA="<<a;
            cout<<"\nB="<<b;
            cout<<"\nC="<<c;
                }
};
class base2
{
    int d;
    protected:
        int e;
    public:
        void input()
        {
            cout<<"Enter D and E\n";
            cin>>d>>e;
        }
        void output()
        {
            cout<<"\nD="<<d;
            cout<<"\nE="<<e;
        }
};
class derived:public base1,public base2
{
    int f;
    protected:
        int g;
    public:
        void getfg()
        {
            cout<<"Enter F and G\n";
            cin>>f>>g;
        }
        void putfg()
        {
            cout<<"\nF="<<f;
            cout<<"\nG="<<g;
        }
};
main()
{
    derived p;
    p.getdata();
    p.input();
    p.getfg();
    p.putdata();
    p.output();
    p.putfg();
}

Output:

Ambiguity:

In Multiple Inheritance There is an Ambiguity That If both Base Classes have same function then which function will be invoked by the object of derived class.
Because both functions are the members of derived class.
Therefore there will be an error message stating that the call is ambiguous.

Solution: 

We can solve the above problem by using scope resolution operator.

p.base1::putdata();

p.base2::putdata();

Error Message:







Multilevel Inheritance

Multilevel:

It is a type of inheritance in which the derived class is used as base class to derive another class.
Thus the second derived class has features of both base and 1 st derived class.

 Program to Illustrate The Multilevel Inheritance:


#include<iostream>
using namespace std;
class base
{
    int a;
    protected:
        int b;
    public:
        int c;
        void getdata();
        void display();
};
void base:: getdata()
{
    cout<<"Enter The Values Of A,B,C\n";
    cin>>a>>b>>c;
}
void base::display()
{
    cout<<"Values Are\n";
    cout<<"A="<<a;
    cout<<"\nB="<<b;
    cout<<"\nC="<<c;
}
class derive:public base
{
    int d;
    protected:
        int e;
    public:
    void displaay()
    {
        cout<<"\nD="<<d;
        cout<<"\nE="<<e;
    }
            void input()
            {
                cout<<"\nEnter D AND E\n";
                cin>>d>>e;
       
            }
};
class derive1:public derive
{
    int f;
    protected:
        int g;
    public:
        void getfg()
        { cout<<"Enter F And G\n";
            cin>>f>>g;
        }
        void putfg()
        {
        cout<<"\nF="<<f;
        cout<<"\nG="<<g;
        }
};

main()
{
    derive1 p;
    p.getdata();
    p.input();
    p.getfg();
    p.display();
    p.displaay();
    p.putfg();
}