Friday, August 26, 2022

INNER CLASS / OUTER CLASS

 # include<iostream>

using namespace std;


class outer

{

class inner;

public:

void fun()

{

i.display();

}

class inner

{

public:

void display()

{

cout<<"display of inner"<<endl;

}

};

inner i;

};

int main()

{

outer::inner i;

}

STATIC DATA MEMBER FUNCATION

 #include <iostream>

using namespace std;

class Student

{

public:

 int roll;

 string name;

 static int addNo;

 Student(string n)

 {

 addNo++;

 roll=addNo;

 name=n;

 }

 void display()

 {

 cout<<"Name "<<name<<endl<<"Roll "<<roll<<endl;

 }

};

int Student::addNo=0;

int main()

{

 Student s1("John");

 Student s2("Ravi");

 Student s3("Khan");

 Student s4("Khan");

 Student s5("Khan");

 Student s6("Khan");



 s1.display();

 s2.display();

 s3.display();

 s4.display();

 s5.display();

 s6.display();

 cout<<"Number Admission "<<Student::addNo<<endl;

}

STATIC FUNCATION

 #include<iostream>

using namespace std;


class test

{

public:

     int a;

     static int count;

     test()

     {

   a=10;

           count++;

     }

     static int getcount()

     {

   return count;

     }

};

int test::count=0;

int main()

{

test t1,t2;

cout<<test::getcount()<<endl;

cout<<t1.getcount()<<endl;

}

FRIENDS FUUCATON

 #include<iostream>

using namespace std;


class your;

class my

{

private:

int a;

protected:

int b;

public:

int c;

friend your;

};

class your

{

public:

my m;

void fun()

{

     m.a=10;

     m.b=10;

     m.c=10;

}

};

Wednesday, August 24, 2022

BINARY SEARCH

 #include <bits/stdc++.h>

using namespace std;

 

int Binary_Search(int Arr[], int low, int high, int key)

{

    while (low <= high) {

        int middle = low + (high - low) / 2;

 

        if (Arr[middle] == key)

            return middle;

        if (Arr[middle] < key)

            low = middle + 1;

 

        else

            high = middle - 1;

    }

 

    return -1;

}

 

int main(void)

{

    int arr[] = {38, 4, 8, 38, 56, 27, 32, 38, 45};

    int len = sizeof(arr) / sizeof(arr[0]);

    int key = 38;

    int result = Binary_Search(arr, 0, len - 1, key);

    if(result == -1)

    cout << "Element not found" ;

    else

    cout << "Element found at index " << result+1;

    return 0;

}

LINEAR SEARCH

 #include <stdio.h>


int linear_search(int arr[], int n, int x)

{

int i;

for(i = 0; i < n; i++)

{

if(arr[i] == x)

return i + 1;

}

return -1;

}


void main()

{

int arr[50], n, i, x, res;

printf("Enter the number of elements in array: ");

scanf("%d", &n);

printf("\nEnter the numbers: ");

for(i = 0; i < n; i++)

scanf("%d", &arr[i]);

printf("\nEnter the number to be searched: ");

scanf("%d", &x);

res = linear_search(arr, n, x);

if(res == -1)

printf("\n%d does not exist in the array.", x);

else

printf("\n%d is present at position %d in the array.", x, res);

}

WRITE A CLASS TO DEMOSTRATE POLYMORPHISM

 #include<iostream>

using namespace std;

class Shape

{

public:

 virtual float area()=0;

 virtual float perimeter()=0;

};

class Rectangle:public Shape

{

private:

 float length;

 float breadth;

public:

 Rectangle(int l=1,int b=1){length=1;breadth=b;}


 float area(){ return length*breadth;}

 float perimeter(){return 2*(length+breadth);}


};

class Circle:public Shape

{

private:

 float radius;

public:

 Circle(float r){radius=r;}

 float area(){return 3.1425*radius*radius;}

 float perimeter(){return 2*3.1425*radius;}

};

int main()

{

 Shape *s=new Rectangle(10,5);

 cout<<"Area of Rectangle "<<s->area()<<endl;

 cout<<"Perimeter of Rectangle "<<s->perimeter()<<endl;


 s=new Circle(10);

 cout<<"Area of Circle "<<s->area()<<endl;

 cout<<"Perimeter of Circle "<<s->perimeter()<<endl;


}

POLYMORPHISM

 #include<iostream>

using namespace std;


class Car

{

public:

 virtual void start()=0;

};

class Innova:public Car

{

public:

 void start(){cout<<"Innova Started"<<endl;}

};

class Swift:public Car

{

public:

 void start(){cout<<"Swift Started"<<endl;}

};

int main()

{

 //Car c;

 Car *p=new Innova();

 p->start();

 p=new Swift();

 p->start();


}

VIRTUAL FUNCATION

 #include<iostream>

using namespace std;


class BasicCar

{

public:

 virtual void start(){cout<<"BasicCar started"<<endl;}

};

class AdvanceCar: public BasicCar

{

public:

 void start(){cout<<"AdvanceCar Started"<<endl;}

};

int main()

{

 BasicCar *p=new AdvanceCar();

 p->start();

}


FUNCATION OVERRIDING

 #include<iostream>

using namespace std;


class base 

{

    public:

    void fun()

    {

        cout<<" fun is a base "<<endl;

    }

};

class dervied : public base 

{

    public:

    void fun()

    {

        cout<<" fun is a dervied "<<endl;

    }

};

int main()

{

    dervied d;

    d.fun();

}

Thursday, August 18, 2022

BASE CLASS POINTER TO DERVIED CLASS OBLECT

 # include<iostream>

using namespace std;

    

/*program for demo#3-base class pointer derived class object


*/

class basicCar

{

public:

void start()

{

cout<<"car started"<<endl;

}

};

class advanceCar:public basicCar

{

public:

void playmusic()

{

cout<<"playing music"<<endl;

}

};

int main()

{

basicCar b;

//advanceCar *q=&b;

advanceCar a;

a.start();

a.playmusic();

basicCar *ptr=&a;

ptr->start();

//ptr->playmusic();

}

QUESTION OF SALARY OF EMPLOYEE

 #include<iostream>

using namespace std;


class employee

{

    private:

    int eid;

    string name;

    public:

    employee(int eid,string name)

    {

        eid=e;

        name=n;

    }

    int getemployeeID()

    {

        return eid;

    }

    string getname()

    {

        return name;

    }

};

class fulltime employee:public employee

{

    private:

    int salary;

    public:

    fulltime employee(int e,sting n,int sal):employee (e,n)

    {

      salary =sal;   

    }

    int getsalary()

    {

        return salary;

    }

};

class parttime employee:public employee

{

    private:

    int wage;

    public:

    parttime employee(int e,string n,int w):employee (e,n)

    {

        wage=w;

    }

    int getwage()

    {

        return wage;

    }

};


int main()

{

    parttime employee p1(1,"john",500);

    fulltime employee p2(2,"hima",400);

    cout<<"Salary of "<<p2.getName()<<" is "<<p2.getSalary()<<endl;

    cout<<"Daily wage of "<<p1.getName()<<" is "<<p1.getWage()<<endl;

}

TYPES OF INHERITANCE

 #include<iostream>

using namespace std;

class Parent

{

private: int a;

protected: int b;

public: int c;


 void funParent()

 {

 a=10;

 b=5;

 c=15;

 }

};

class Child: private Parent

{

private:


protected:


public:


 void funChild()

 {

 //a=10;

 b=5;

 c=15;

 }

};

class GrandChild : public Child

{

public:

 void funGrandChild()

 {

 //a=10;

 //b=5;

 //c=20;

 }

};

int main()

{

 // Child c;

 //c.a=10;

 //c.b=5;

 //c.c=20;

}

DATA STUCTURE

 #include <bits/stdc++.h>

using namespace std;



void traversal(int A[],int N);

void insertion(int A[],int &N,int Pos,int ITEM);

int lsearch(int A[],int N,int item);

void deletee(int A[],int &N,int Pos,int ITEM );

int main()

{


int a[10],n,pos,item,choice,delitem;

char ch;

cout<<"\n Enter size:";

cin>>n;

cout<<"\n Enter Array:";

for(int i=0;i<n;i++)

cin>>a[i];

do

{


cout<<"\n \t\t Menu Driven Program";

cout<<"\n 1. Traversal \n 2. Insertion \n 3. Deletion \n 4. Searching";

cout<<"\n Enter your choice:";

cin>>choice;

switch(choice)

{

        case 1: traversal(a,n);

                break;

        case 2:

                cout<<"\n Enter Position:";

                cin>>pos;

                cout<<"\nEnter Element:";

                cin>>item;

                insertion(a,n,pos,item);

                break;

        case 3: cout<<"\n Enter Element to be deleted:";

                cin>>delitem;

                pos=lsearch(a,n,delitem);

                if(pos==-1)

                {

                cout<<"\n Element not found";

                }

                deletee(a,n,pos,delitem);

                break;

        case 4: cout<<"\n Enter the element to be searched";

                cin>>item;

                pos=lsearch(a,n,item);

                cout<<"\n Element found at index:"<<pos;

                cout<<"\n Element found at position"<<pos+1;

                break;

        default:cout<<"wrong choice";

        }

        cout<<"\n Do you want to continue?";

        cout<<"(Press y/n)";

        cin>>ch;

        }while(ch=='y');


}

void traversal(int b[],int N)

{

for(int i=0;i<N;i++)

cout<<b[i]<<" ";

}

void insertion(int A[],int &N,int Pos,int ITEM)

{

for(int i=N;i>=Pos;i--)

A[i+1]=A[i];

A[Pos]=ITEM;

N=N+1;

}

void deletee(int A[],int &N,int Pos,int ITEM)

{

for(int i=Pos;i<N;i++)

A[i]=A[i+1];

N=N-1;

cout<<ITEM<<" is deleted from the array.";

}

int lsearch(int A[],int N,int ITEM)

{

int index=-1;

for(int i=0;i<N;i++)

{

if(A[i]==ITEM)

{

index=i;

}

}

return index;

}

Wednesday, August 17, 2022

EXAMPLE OF INHERITENCE

 #include<iostream>

using namespace std;


class Rectangle

{

private:

 int length;

 int breadth;

public:

 Rectangle();

 Rectangle(int l,int b);

 Rectangle(Rectangle &r);

 int getLength(){return length;}

 int getBreadth(){return breadth;}

 void setLength(int l);

 void setBreadth(int b);

 int area();

 int perimeter();

 bool isSquare();

 ~Rectangle();

};

class Cuboid:public Rectangle

{

private:

 int height;

public:

 Cuboid(int h)

 {

 height=h;

 }

 int getHeight(){return height;}

 void setHeight(int h){height=h;}

 int volume(){return getLength()*getBreadth()*height;}

};

int main()

{

 Cuboid c(5);

 c.setLength(10);

 c.setBreadth(7);

 cout<<"Volume is "<<c.volume()<<endl;

}

Rectangle::Rectangle()

{

 length=1;

 breadth=1;

}

Rectangle::Rectangle(int l,int b)

{

 length=l;

 breadth=b;

}

Rectangle::Rectangle(Rectangle &r)

{

 length=r.length;

 breadth=r.breadth;

}

void Rectangle::setLength(int l)

{

 length=l;

}

void Rectangle::setBreadth(int b)

{

 breadth=b;

}

int Rectangle::area()

{

 return length*breadth;

}

int Rectangle::perimeter()

{

 return 2*(length+breadth);

}

bool Rectangle::isSquare()

{

 return length==breadth;

}

Rectangle::~Rectangle()

{

 // cout<<"Rectangle Destroyed";

}

INHERTENCE

#include<iostream>

using namespace std;


class rectangle

{

    private:

    int length;

    int breadth;

    public:

    rectangle(int r=0, int b=0);

    int getlength;

    int getbreadth;

    void setlength(int l);

    void setbreadth(int b);

    int area();

    int perimeter();

};

class cuboid:public rectangle

{

    private:

    int height;

    public:

    cubiod(int L=0,int b=0,int h=0)

    {

        height=h;

        setlength(L);

        setbreadth(b);

    }

int getheight();

void setheigth(int h);

int volume()

{

    return getlength()*getbreadth()*heigth;

}

};

int  main()

{

    cubiod c(10,20,30);

    cout<<c.getlength();

    cout<<c.volume();

    cout<<c.area();

    cout<<c;

}

1. INHERITENCE

 #include<iostream>

using namespace std;


class Base

{

    public:

    int x;

    void show()

    {

      cout<<x;  

    }

};

class dervied : public Base

{

    public:

    int y;

    void show()

    {

        cout<<x<<" "<<y;

        d.display();

    }

};

int main()

{

    base b

    b.x=25;

    b.show();

    

    dervied d;

    d.x=15;

    d.y=15;

    d.show();

    d.display();

}

Tuesday, August 16, 2022

JAVA CONSTRUCTURE

 public class student

{
int id;
String name;
String rollno;
student(int i, String n, String r)
{
id=i;
name= n;
rollno= r.toString();
}
student(student s1)
{
id=s1.id;
name=s1.name;
rollno=s1.rollno;
}
void display()
{
System.out.println(id+" "+name+" "+rollno);
}
public static void main(String[] args)
{
student obj1=new student(2197,"Himanshu","21bcs5642");
student obj2=new student(obj1);
obj1.display();
obj2.display();
}
}

WRITE A RATIONAL NO WITH OPERATOR + AND << OPERATOR

 #include<iostream>

using namespace std;

class Rational

{

private:

 int p;

 int q;

public:

 Rational()

 {

 p=1;

 q=1;

 }

 Rational(int p,int q)

 {

 this->p=p;

 this->q=q;

 }

 Rational(Rational &r)

 {

 this->p=r.p;

 this->q=r.q;

 }

 int getP(){return p;}

 int getQ(){return q;}

 void setP(int p)

 {

 this->p=p;

 }

 void setQ(int q)

 {

 this->q=q;

 }

 Rational operator+(Rational r)

 {

 Rational t;

 t.p=this->p*r.q+this->q*r.p;

 t.q=this->q*r.q;

 return t;

 }

 friend ostream & operator<<(ostream &os,Rational &r);

};ostream & operator<<(ostream &os,Rational &r)

{

 os<<r.p<<"/"<<r.q;

 return os;

}

int main()

{

 Rational r1(3,4),r2(2,5),r3;


 r3=r1+r2;

 cout<<"Sum of "<<r1<<" and "<<r2<<" is "<<r3<<endl;


}

Tuesday, August 9, 2022

WRITING A CODE IN C++ FOR STUDENT MARKS

 #include<iostream>

using namespace std;

class Student

{

private:

 int roll;

 string name;

 int math marks;

 int phyMarks;

 int chemMarks;

public:

 Student(int r,string n,int m,int p,int c)

 {

 roll=r;

 name=n;

 mathMarks=m;

 phyMarks=p;

 chemMarks=c;

 }

 int total()

 {

 return mathMarks+phyMarks+chemMarks;

 }

 char grade()

 {

 float average=total()/3;

 if(average > 60)

 return 'A';

 else if(average>=40 && average<60)

 return 'B';

 else

 return 'C';

 }


};

int main()

{

 int roll;

 string name;

 int m,p,c;

 cout<<"Enter Roll number of a Student: ";

 cin>>roll;

 cout<<"Enter Name of a Student:";

 cin>>name;

 cout<<"Enter marks in 3 subjects";

 cin>>m>>p>>c;

 Student s(roll,name,m,p,c);

 cout<<"Total Marks:"<<s.total()<<endl;

 cout<<"Grade of Student:"<<s.grade()<<endl;


}

ALL FUNCATION OF A CLASS

 #include<iostream>

using namespace std;


class Rectangle

{

private:

 int length;

 int breadth;

public:

 Rectangle();

 Rectangle(int l,int b);

 Rectangle(Rectangle &r);

 int getLength(){return length;}

 int getBreadth(){return breadth;}

 void setLength(int l);

 void setBreadth(int b);

 int area();

 int perimeter();

 bool isSquare();

 ~Rectangle();

};

int main()

{

 Rectangle r1(10,10);

 cout<<"Area "<<r1.area()<<endl;

 if(r1.isSquare())

 cout<<"Yes"<<endl;

}

Rectangle::Rectangle()

{

 length=1;

 breadth=1;

}

Rectangle::Rectangle(int l,int b)

{

 length=l;

 breadth=b;

}

Rectangle::Rectangle(Rectangle &r)

{

 length=r.length;

 breadth=r.breadth;

}

void Rectangle::setLength(int l)

{

 length=l;

}

void Rectangle::setBreadth(int b)

{

 breadth=b;

}

int Rectangle::area()

{

 return length*breadth;

}

int Rectangle::perimeter()

{

 return 2*(length+breadth);

}

bool Rectangle::isSquare()

{

 return length==breadth;

}

Rectangle::~Rectangle()

{

 cout<<"Rectangle Destroyed";

}

DEEP COPY CONSTUCTURE

 #include<iostream>

using namespace std;


class rectangle

{

    private:

    int length;

    int breadth;

    public:

    rectangle()

    {

        cout<<"Enter the length:";

        cin>>length;

        cout<<"Enter the breadth:";

        cin>>breadth;

    }

    void setlength(int l)

    {

        if(l>=0)

        {

            length=l;

        }

        else

        {

            length=0;

        }

    }

    void setbreadth(int b)

    {

        if(b>=0)

        {

            breadth=0;

        }

        else

        {

            breadth=0;

        }

    }

    int getlength()

    {

        return length;

    }

    int getbreadth()

    {

        return breadth;

    }

    int area()

    {

        return length*breadth;

    }

    int perimeter()

    {

        return 2*(length+breadth);

    }

};

int main()

{

    rectangle r1,r2;

    cout<<"The area of r1 is:"<<r1.area()<<endl;

    cout<<"The perimeter r1 is:"<<r1.perimeter()<<endl;

    cout<<"The area of r2 is:"<<r2.area()<<endl;

    cout<<"The perimeter of r2 is:"<< r2.perimeter()<<endl;

}

Friday, August 5, 2022

COPY CONSTRUCTOR

 


#include <iostream>

using namespace std;

 

class Rectangle {

  private:

    double length;

    double breadth;

 

  public:

    Rectangle(double l, double b) {

      length = l;

      breadth = b;

    }

    Rectangle(Rectangle &obj) {

      length = obj.length;

      breadth = obj.breadth;

    }

 

 

    double calculateArea() {

      return length * breadth;

    }

};

 

int main() {

Rectangle obj1(10,6);

Rectangle obj2 = obj1; 

 

  cout << "Area of Rectangle 1: " << obj1.calculateArea();

  cout << "Area of Rectangle 2: " << obj2.calculateArea();

 

  return 0;

}

DATA HIDING OWN CODE FOR RECTANGLE AREA

 #include<iostream>

using namespace std;


class rectangle

{

    private:

    int length;

    int breadth;

    public:

    void setlength(int l)

    {

        if(l>=0)

        {

           length=l; 

        }

        else

        {

            length=0;

        }

    }

    void setbreadth(int b)

    {

        if(b>=0)

        {

            breadth=b;

        }

        else

        {

            breadth=0;

        }

    }

    int getlength()

    {

        return length;

    }

    int getbreadth()

    {

       return breadth;

    }

    int area()

    {

        return length*breadth;

    }

    int perimeter()

    {

        return 2*(length+breadth);

    }

};

int main()

{

   rectangle r1,r2;

   r1.setlength(20);

   r1.setbreadth(30);

   cout<<"The area r1 is :"<<r1.area()<<endl;

   cout<<"The perimeter r1 is:"<<r1.perimeter()<<endl;

   cout<<"------------------------------------------------"<<endl;

   r2.setlength(50);

   r2.setbreadth(80);

   cout<<"The area r2 is:"<<r2.area()<<endl;

   cout<<"The perimeter r2 is:"<<r2.perimeter()<<endl;

}

Thursday, August 4, 2022

CREATING A OBJECT IN HEAP USING POINTER

 #include<iostream>

using namespace std;


class rectangle

{

    public:

    int length;

    int breadth;

    int area()

    {

        return length*breadth;

    }

    int perimeter()

    {

        return 2*(length+breadth);

    }

    int sum()

    {

        return length+breadth;

    }

    int rec()

    {

        return 4*(length+breadth);

    }

};

int main()

{

    rectangle *p;

    p=new rectangle;

    rectangle *q=new rectangle();

    p->length=20;

    p->breadth=30;

    cout<<p->area()<<endl;

    cout<<p->perimeter()<<endl;

}

WRITING A OWN CLASS

 #include<iostream>

using namespace std;


class rectangle

{

    public:

    int length;

    int breadth;

    int area()

    {

        return length*breadth;

    }

    int perimeter()

    {

        return 2*(length+breadth);

    }

    int sum()

    {

        return length+breadth;

    }

    int rec()

    {

        return 4*(length+breadth);

    }

};

int main()

{

    rectangle r1,r2;

    r1.length=10;

    r1.breadth=20;

    cout<<"The area r1 is :"<<r1.area()<<endl;

    cout<<"The perimeter of r1 is :"<<r1.perimeter()<<endl;

    cout<<"The sum r1 is : "<<r1.sum()<<endl;

    cout<<"The rec r1 is : "<<r1.rec()<<endl;

    r2.length=20;

    r2.breadth=30;

    cout<<"The area of r2 is :"<<r2.area()<<endl;

    cout<<"The perimeter of r2 is :"<<r2.perimeter()<<endl;

    cout<<"The sum r2 is : "<<r2.sum()<<endl;

    cout<<"The rec r2 is : "<<r2.rec()<<endl;

}

WRITING A CLASS

 #include<iostream>

using namespace std;


class rectangle

{

    public:

    int length;

    int breadth;

    int area()

    {

        return length*breadth;

    }

    int perimeter()

    {

        return 2*(length+breadth);

    }

};

int main()

{

    rectangle r1,r2;

    r1.length=10;

    r1.breadth=20;

    cout<<"The area r1 is :"<<r1.area()<<endl;

    cout<<"The perimeter of r1 is :"<<r1.perimeter()<<endl;

    r2.length=20;

    r2.breadth=30;

    cout<<"The area of r2 is :"<<r2.area()<<endl;

    cout<<"The perimeter of r2 is :"<<r2.perimeter()<<endl;

}

POINTER TO FUNCATION

 #include<iostream>

using namespace std;


void display()

{

    cout<<"hello"<<endl;

}

int main()

{

    void(*fp)();

    fp=display;

    (*fp)();

}

Wednesday, August 3, 2022

LINEAR SEARCH USING FUNCATION

 #include<iostream>

using namespace std;

int Search(int A[],int n,int key)

{

 for(int i=0;i<n;i++)

 if(key==A[i])

 return i;

 return 0;

}

int main()

{

 int A[]={2,4,5,7,10,9,13};

 int k;

 cout<<"Enter an Element to be Searched:";

 cin>>k;

 int index=Search(A,7,k);


 cout<<"Element found at index :"<<index<<endl;



}

RECURSIVE FUNCATION

#include<iostream>

using namespace std;



void fun( int n)

{

    if(n>0)

    {

        fun(n-1);

        cout<<n<<endl;

    }

}

int main()

{

    int x=25;

    fun(x);

}

STATIC VARIABLE

 #include<iostream>

using namespace std;



void fun()

{

    static int s=10;

    s++;

    cout<<s<<endl;

}

int main()

{

    fun();

    fun();

    fun();

    fun();

    fun();

    fun();

}

GLOBAL AND LOCAL VARIABLE

 #include<iostream>

using namespace std;


int g=5;

void fun()

{

    int a=10;

    a++;

    g++;

    cout<<a<<" "<<g<<endl;

}

int main()

{

    cout<<g<<endl;

    fun();

    cout<<g<<endl;

}

RETURN BY ADDRESS

 #include<iostream>

using namespace std;


int *fun(int n)

{

int *p=new int[n];

for(int i=0;i<n;i++)

p[i]=i+1;

return p;

}

int main()

{

int *ptr=fun(15);

for(int i=0;i<15;i++)

cout<<i<<endl;

}

CALL BY REFERENCE

 #include<iostream>

using namespace std;


void swap(int &a,int &b)

{

    cout<<&a<<" "<<&b;

    int temp;

    temp=a;

    a=b;

    b=temp;

}


int main()

{

    int x=10; int y=20;

    swap(x,y);

    cout<<&x<<" "<<&y<<endl;

    cout<<x<<" "<<y<<endl;

}

CONSTRUCTURE IN PARENT CLASS

class Parent {     public Parent()     {         System.out.println("Parent Constrcutor");     }              } class Child extend...