iostream operators (<>) Overloading;

Question – Demonstrate the overloading of istream keyword cin >> and ostream keyword cout<< :

Answer :- Here in the program we use to make operator function as a friend of a class because it will be called without creating a object;

 

#include<iostream>
#include<conio.h>
using namespace std;
class inc
{
    private:
        int count ;
    public:
        inc(int x)
        {
            count=x;
        }
        inc()
        {
            count=0;
        }
        friend istream & operator >>(istream &ip, inc &i)
        {
            ip>>i.count;
            return ip;
        }
        friend ostream & operator <<(ostream &op, inc &j)
        {
            op<<j.count;
            return op;
        }
};
int main()
{
    inc obj1(8),obj2,obj3;
    cout<<"\nENTER THE VALUE IN OBJ3 : ";
    cin >> obj3;
    cout<<"\nVALUE IN OBJ1 : ";
    cout << obj1;
    cout<<"\nVALUE IN OBJ2 : ";
    cout<< obj2;
    cout<<"\nVALUE IN OBJ3 : ";
    cout << obj3;
    cout<<"\n\nPROGRAMMING @C#ODE STUDIO";
    getch();
}

 

op_ovr2

FUNCTION OVERRIDING;

Function Overriding – If base class and derived class having functions of same name,argument and return type then the object to derive class will invoke the function initialized in derive class this properties in c++ is known as Op Overriding.

To overcome this problem we use REVOLVING AMBIGUITY concept. You will see in next post.

Program-

 

#include<iostream.h>
#include<conio.h>
using namespace std;
class base
{
    int day,mon,yr;
    public: 
        void getb(int dd, int mm, int yy)
        {
            day=dd;
            mon=mm;
            yr=yy;
        }
        void disp()
        {
            cout<<"\nDay= "<<day
                <<"\tMonth= "<<mon
                <<"\tYear= "<<yr;
        }
};
class derive: public base //derive class to base
{
    int hr,min;
    public:
        void getd(int h,int m, int d, int mo, int y)
        {
            getb(d,mo,y);
            hr=h;
            min=m;
        }
        void disp()
        {
            cout<<"\nHours= "<<hr<<"\tMinutes= "<<min;
            
        }
};
int main()
{
    derive sd;
    sd.getd(10,58,22,10,2013);
     sd.disp();  //it will invoke the function of derive class
    cout<<"\n\nPROGRAMMING @C#ODE STUDIO";
    getch();
}

 

overrid

Aggregation:

Aggregation – Here when we make two or more classes, and initialize the object of one class in another class and take the input through the 2nd class. when we delete the object of 2nd class it doesnt affect the object of 1st class which is initialized in 2nd class :

program-

#include<iostream.h>

#include<conio.h>

#include<string.h>

using namespace std;

class date

{

int day,mon,year;

public:

date(int dd, int mm, int yy)

{

day=dd;

mon=mm;

year=yy;

}

void display()

{

cout<<“\n–DATE OF BIRTH–“;

cout<<“\nDay : “<<day

<<“\tMonth : “<<mon

<<“\tYear : “<<year;

}

};

class student

{

int rn;

char name[25];

date dob;

public:

student(int r, char n[], int d, int m, int y):dob(d,m,y)

{

rn=r;

strcpy(name,n);

}

void display()

{

cout<<“\nRoll No. : “<<rn

<<“\nName : “<<name;

dob.display();

}

};

int main()

{

int rl,da,mo,ye;

char nm[10];

cout<<“\nNAME : “;

cin>>nm;

cout<<“\nROLL NO.: “;

cin>>rl;

cout<<“\nDAY : “;

cin>>da;

cout<<“\nMONTH : “;

cin>>mo;

cout<<“\nYEAR : “;

cin>>ye;

student *obj1=new student(rl,nm,da,mo,ye);

obj1->display();

cout<<“\n\n–WHEN OBJECT OF BASE CLASS IS DELETED–\n\n”;

delete obj1; //deleting object of class doesnt affect the object of date class;

obj1->display(); //it gives garbage value only for variables initi. in student class;

cout<<“\n\nPROGRAMMING @ C#ODE STUDIO “;

getch();

}

AGGR

HERARICHAL INHERITANCE;

QUES- MAKE A CLASS WITH NAME STUDENT WITH DATA NAME ROLL AND DERIVE TWO CLASS SPORTS AND TEST FROM BASE CLASS. GET SCORE IN SPORTS AND MARKS OF 2 SUBJECT IN TEST. PRINT SCORE AND AVERAGE MARKS OF 2 SUBJECTS;

ANSWER-

#include<iostream.h>

#include<conio.h>

using namespace std;

class student //BASE CLASS

{

int roll;

char name[20];

protected:

void getdata()

{

cout<<“\nINPUT NAME:”;

cin>>name;

cout<<“\nINPUT ROLL NO.”;

cin>>roll;

}

void output()

{

cout<<“\nSTUDENT INFORMATION”;

cout<<“\n=====================”;

cout<<“\nName : “<<name<<“\nRoll No.: “<<roll;

}

};

class sports: public student //DERIVED CLASS TO BASE CLASS WITH PUBLIC ACCESS SPECIFIERS

{

int score;

public:

void getscore()

{

getdata(); //A DERIVE CLASS CAN CALL THE FN. DIRECTLY OF ITS BASE CLASS.

cout<<“\nINPUT SCORE IN SPORT :”;

cin>>score;

}

void putscore()

{

output();

cout<<“\nSCORE IN SPORTS IS : “<<score;

}

};

class test: public student //ANOTHER DERIVE CLASS IN STUDENT WITH PUBLIC ACCESS SPEC.

{

int mark1,mark2;

public:

void getmark()

{

cout<<“\nINPUT MARKS 1 IN TEST :”;

cin>>mark1;

cout<<“\nINPUT MARKS 2 IN TEST :”;

cin>>mark2;

}

void putmark()

{

cout<<“\nMARKS 1 IS : “<<mark1;

cout<<“\nMARKS 2 IS : “<<mark2;

cout<<“\nAVERAGE OF MARK1 & MARK2 :”<<(mark1+mark2)/2;

cout<<“\n\nPROGRAMMING @C#ODE STUDIO”;

}

};

int main()

{

test t;

sports s;

s.getscore();

t.getmark();

s.putscore();

t.putmark();

getch();

}

herar.png

MULTILEVEL INHERITANCE;

QUE- CREATE A CLASS WITH DATA DAY, MONTH, YEAR AND A DERIVE CLASS TO BASE WITH DATA HOUR AND MINUTE, MAKE A SUB DERIVE CLASS AND CALL THE FUNCTION USING LATEST CREATED CLASS’s OBJECT-

ANSWER-

#include<iostream.h>

#include<conio.h>

using namespace std;

class base
{

int day,mon,yr;

protected:

void getb(int dd, int mm, int yy)

{

day=dd;

mon=mm;

yr=yy;

}

void disp()

{

cout<<“\nDay= “<<day

<<“\tMonth= “<<mon

<<“\tYear= “<<yr;

}

};

class derive: public base //derive class to base

{

int hr,min;

public:

void getd(int h,int m, int d, int mo, int y)

{

getb(d,mo,y);

hr=h;

min=m;

}

void display()

{

disp();

cout<<“\nHours= “<<hr<<“\tMinutes= “<<min;

cout<<“\n\nPROGRAMMING @C#ODE STUDIO”;

}

};

class subderive:public derive //subdrive class to base

{

public:

void displ()

{

display();

}

};

int main()

{

subderive sd;

sd.getd(10,58,22,10,2013);

sd.displ();

getch();

}

multilevel

MULTIPLE INHERITANCE;

Question:-

Create a class which contains two variables A and B, and another class which contains C and D. Then make another class and inherit that in first two class give multiplication of C n D and Addition of A n B.

Answer:-

#include<iostream>
#include<conio.h>
using namespace std;
class A
{

int a,b;
public:

void getab()
{

cout<<“\nINPUT A:”;
cin>>a;
cout<<“\nINPUT B:”;
cin>>b;

}
void dispab()
{

cout<<“\nA : “<<a;
cout<<“\nB : “<<b;

}
int outa()
{

return a;

}
int outb()
{

return b;

}

};
class B
{

int c,d;
public:

void getcd()
{

cout<<“\nINPUT C:”;
cin>>c;
cout<<“\nINPUT D:”;
cin>>d;

}
void dispcd()
{

cout<<“\nC : “<<c;
cout<<“\nD : “<<d;

}
int outc()
{

return c;

}
int outd()
{

return d;

}

};
class D:public B,public A   //class D is inherited in Class A and Class B
{

int sum,mul;
public:

void get()
{

getab();
getcd();
sum=outa()+outb();
mul=outc()*outd();

}
void disp()
{

dispab();
dispcd();
cout<<“\nADDITION OF A AND B : “<<sum;
cout<<“\nMULTIPLICATION OF C AND D : “<<mul;

}

};
int main()
{

D obj;
obj.get();
obj.disp();
cout<<“\n\nPROGRAMMING AT C#ODE STUDIO”;
getch();

}

multiplenh

HYBRID INHERITANCE;

QUESTION=  WAP TO CREATE A CLASS WITH NAME, STUDENT AND INHERIT TWO CLASS
NAME SPORT AND TEST IN IT WITH FN’s. GETSCORE,GETMARKS,PUTSCORE,PUTMARKS.
SHOW AVERAGE OF ALL THREE MARKS AND SCORE IN CLASS RESULT INHERITED IN
SPORT AND TEST..
ANSWER=
#include<iostream.h>
#include<conio.h>
using namespace std;
class student
{
int roll;
char name[20];
public:
void getdata()
{
cout<<“\nINPUT NAME:”;
cin>>name;
cout<<“\nINPUT ROLL NO.”;
cin>>roll;
}
void output()
{
cout<<“\nSTUDENT INFORMATION”;
cout<<“\n=====================”;
cout<<“\nName : “<<name<<“\nRoll No.: “<<roll;
}
};
class sports: public student
{
int score;
public:
void getscore()
{
getdata();
cout<<“\nINPUT SCORE IN SPORT :”;
cin>>score;
}
void putscore()
{
output();
cout<<“\nSCORE IS : “<<score;
}
int scr()
{
return score;
}
};
class test: public student
{
int mark1,mark2;
public:
void getmark()
{
cout<<“\nINPUT MARKS 1 IN TEST :”;
cin>>mark1;
cout<<“\nINPUT MARKS 2 IN TEST :”;
cin>>mark2;
}
void putmark()
{
cout<<“\nMARKS 1 IS : “<<mark1;
cout<<“\nMARKS 2 IS : “<<mark2;
}
int mrk()
{
return mark1;
}
int mrks()
{
return mark2;
}
};
class result: public sports, public test
{
int avg;
public:
void average()
{
getscore();
getmark();
avg=scr()+mrk()+mrks();
avg=avg/3;
}
void display()
{
putscore();
putmark();
cout<<“\nAVERAGE OF SCORE AND MARK IS : “<<avg;
cout<<“\n\nPROGRAMMING AT C#ODE STUDIO”;
}
};
int main()
{
result obj;
obj.average();
obj.display();
getch();
}
INHER