C++ Classes and Operator Overloading, Assignments of Object Oriented Programming

This document showcases a c++ code snippet demonstrating the concept of classes, constructors, and operator overloading. The code defines two classes 'a' and 'b' with their respective data members and member functions. The 'a' class includes an integer member 'c' and defines constructors, display function, pre-increment operator, and post-decrement operator. The 'b' class includes an integer member 'd' and defines constructors, display function, and pre-increment operator. The main function initializes instances of both classes, performs various operations, and displays the results.

Typology: Assignments

2020/2021

Uploaded on 05/26/2021

aniruddh-singh-1
aniruddh-singh-1 🇮🇳

5

(1)

4 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CODE:
#include <iostream>
using namespace std;
class a
{
int c;
public:
a()
{
c=10;
}
void disp()
{
cout<<"c="<<c<<endl;
}
void operator ++(int)
{
c++;
}
void operator +()
{
++c;
}
friend a operator --(a & p);
};
a operator --(a & p)
{
a z;
z.c=p.c*p.c;
return z;
}
class b
{
int d; public:
b()
{
d=20;
}
void disp()
{
cout<<"d="<<d<<endl;
}
friend b operator ++(b & e);
};
pf2

Partial preview of the text

Download C++ Classes and Operator Overloading and more Assignments Object Oriented Programming in PDF only on Docsity!

CODE:

#include using namespace std; class a { int c; public: a() { c=10; } void disp() { cout<<"c="<<c<<endl; } void operator ++(int) { c++; } void operator +() { ++c; } friend a operator --(a & p); }; a operator --(a & p) { a z; z.c=p.c*p.c; return z; } class b { int d; public: b() { d=20; } void disp() { cout<<"d="<<d<<endl; } friend b operator ++(b & e); };

b operator ++(b & e) { b q; q.d=e.d*e.d; return q; } main() { a x; a y; x.disp(); y= operator --(x); x.disp(); cout<<"y"<<endl; y.disp(); x++ ; x.disp(); +x; x.disp(); b u; b t; u.disp(); t=operator ++(u); t.disp(); }

OUTPUT: