Primer #1 Preklapanje Operatora Koristeći Funkciju Članicu
Primer #1 Preklapanje Operatora Koristeći Funkciju Članicu
Primer #1 Preklapanje Operatora Koristeći Funkciju Članicu
double getVolume(void) {
return length * breadth * height;
}
void setLength( double len ) {
length = len;
}
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
#include <iostream>
using namespace std;
class Distance{
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// preklopi poziv funkcije
Distance operator()(int a, int b, int c) {
Distance D;
// just put random calculation
D.feet = a + c + 10;
D.inches = b + c + 100 ;
return D;
}
2
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main(){
Distance D1(11, 10), D2;
return 0;
}
class safearay{
private:
int arr[SIZE];
public:
safearay() {
register int i;
for(i = 0; i < SIZE; i++) {
arr[i] = i;
}
}
int &operator[](int i) {
if( i > SIZE ) {
cout << "Index out of bounds" <<endl;
// return first element.
return arr[0];
}
return arr[i];
}
};
int main(){
safearay A;
cout << "Value of A[2] : " << A[2] <<endl;
cout << "Value of A[5] : " << A[5]<<endl;
cout << "Value of A[12] : " << A[12]<<endl;
return 0;
}
3
Primer #4 Operator (->) se moe preklopiti
#include <iostream>
#include <vector>
using namespace std;
// Consider an actual class.
class Obj {
static int i, j;
public:
void f() const { cout << i++ << endl; }
void g() const { cout << j++ << endl; }
};
// Static member definitions:
int Obj::i = 10;
int Obj::j = 12;
4
int main() {
const int sz = 10;
Obj o[sz];
ObjContainer oc;
for(int i = 0; i < sz; i++) {
oc.add(&o[i]);
}
SmartPointer sp(oc); // Create an iterator
do {
sp->f(); // smart pointer call
sp->g();
} while(sp++);
return 0;
}
5
Primer #6 Preklapanje relacionih operatora.
#include <iostream>
using namespace std;
class Distance{
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}
// overloaded minus (-) operator
Distance operator- () {
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
// overloaded < operator
bool operator <(const Distance& d) {
if(feet < d.feet)
{
return true;
}
if(feet == d.feet && inches < d.inches) {
return true;
}
return false;
}
};
int main(){
Distance D1(11, 10), D2(5, 11);
if( D1 < D2 ) {
cout << "D1 is less than D2 " << endl;
}
else {
cout << "D2 is less than D1 " << endl;
}
return 0;
}
6
Primet #7 minus (-) operator se moe preklopiti
#include <iostream>
using namespace std;
class Distance{
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}
// overloaded minus (-) operator
Distance operator- () {
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};
int main(){
Distance D1(11, 10), D2(-5, 11);
-D1; // apply negation
D1.displayDistance(); // display D1
-D2; // apply negation
D2.displayDistance(); // display D2
return 0;
}
7
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
void operator=(const Distance &D ) {
feet = D.feet;
inches = D.inches;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main(){
Distance D1(11, 10), D2(5, 11);
cout << "First Distance : ";
D1.displayDistance();
cout << "Second Distance :";
D2.displayDistance();
return 0;
}
Primer #9 operatori + i =
#include <iostream>
class example{
public:
int a;
int b;
example operator+(const example& obj);
void operator=(const example& obj);
};
void example::operator=(const example& obj){
(*this).a = obj.a;
(*this).b = obj.b;
return;
}
8
example example::operator+(const example& obj2){
example tmp_obj = *this;
tmp_obj.a = tmp_obj.a + obj2.a;
tmp_obj.b = tmp_obj.b + obj2.b;
return tmp_obj;
}
int main(void){
example obj1, obj2, obj3;
obj1.a = 1;
obj1.b = 1;
obj2.a = 2;
obj2.b = 2;
obj3.a = 0;
obj3.b = 0;
std::cout<<obj3.a<<" "<<obj3.b<<"\n";
return 0;
}
Primer#10 //Inkrement i dekrement preklapanje
#include<iostream>
using namespace std;
class Inc {
private:
int count ;
public:
Inc() {
//Default constructor
count = 0 ;
}
Inc(int C) {
// Constructor with Argument
count = C ;
}
Inc operator ++ () {
// Operator Function Definition
return Inc(++count);
}
9
Inc operator -- () {
// Operator Function Definition
return Inc(--count);
}
void display(void) {
cout << count << endl ;
}
};
void main(void) {
Inc a, b(4), c, d, e(1), f(4);
++a;
b++;
c = ++a;
d = b++;
--e;
f--;
cout << "After using the operator --()\n";
cout << "e = ";
e.display();
cout << "f = ";
f.display();
cin.get();
}
10
Rezultat
Before using the operator ++()
a=0
b=4
After using the operator ++()
a=1
b=5
Result prefix (on a) and postfix (on b)
c=2
d=6
Before using the operator --()
e=1
f=4
After using the operator --()
e=0
f=3
Primer #11
#include <iostream>
using namespace std;
class Date{
int mo, da, yr;
public:
Date(int m, int d, int y) {
mo = m; da = d; yr = y;
}
friend ostream& operator<<(ostream& os, const Date& dt);
};
int main(){
Date dt(5, 6, 92);
cout << dt;
}
11