Created
November 7, 2012 22:49
-
-
Save nadinengland/4035102 to your computer and use it in GitHub Desktop.
Virtual & Concrete operators in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// virtual_operators.cpp | |
// Virtual & Concrete operators in C++ | |
// | |
// Created by Thomas Nadin on 07/11/2012. | |
// Copyright (c) 2012 Thomas Nadin. All rights reserved. | |
// | |
#include <iostream> | |
// Base class that all Test classes derive from. | |
class Base { | |
int value_; | |
public: | |
Base(int value = 0) { value_ = value; } | |
int value() { return value_; } | |
void setValue(int value) { value_ = value; } | |
}; | |
// Provides assignment operator helper | |
class ConcreteOperator : public Base { | |
public: | |
ConcreteOperator(int value = 0) : Base(value) { } | |
void operator = (int value) { this->setValue(value); } | |
}; | |
// Double's value | |
class DConcreteOperator : public ConcreteOperator { | |
public: | |
DConcreteOperator(int value = 0) : ConcreteOperator(value) { } | |
void operator = (int value) { this->setValue(value * 2); } | |
}; | |
// Virtual Operator Class | |
class VirtualOperator : public Base { | |
public: | |
VirtualOperator(int value = 0) : Base(value) { } | |
virtual void operator = (int value) { this->setValue(value); } | |
}; | |
class DVirtualOperator: public VirtualOperator { | |
public: | |
DVirtualOperator(int value = 0) : VirtualOperator(value) { } | |
virtual void operator = (int value) { this->setValue(value * 2); } | |
}; | |
// Main: run the tests | |
int main(int argc, const char * argv[]) | |
{ | |
std::cout << "Assignment Operator Testings!" << std::endl; | |
ConcreteOperator concrete_op(5); | |
std::cout << "ConcreteOperator" << std::endl; | |
std::cout << "Constructor: \t" << concrete_op.value() << std::endl; | |
concrete_op = 10; | |
std::cout << "Assignment: \t" << concrete_op.value() << std::endl; | |
std::cout << std::endl; | |
DConcreteOperator dconcrete_op(5); | |
std::cout << "DConcreteOperator" << std::endl; | |
std::cout << "Constructor: \t" << dconcrete_op.value() << std::endl; | |
dconcrete_op = 10; | |
std::cout << "Assignment: \t" << dconcrete_op.value() << std::endl; | |
std::cout << std::endl; | |
VirtualOperator virtual_op(5); | |
std::cout << "VirtualOperator" << std::endl; | |
std::cout << "Constructor: \t" << virtual_op.value() << std::endl; | |
virtual_op = 10; | |
std::cout << "Assignment: \t" << virtual_op.value() << std::endl; | |
std::cout << std::endl; | |
DVirtualOperator dvirtual_op(5); | |
std::cout << "DVirtualOperator" << std::endl; | |
std::cout << "Constructor: \t" << dvirtual_op.value() << std::endl; | |
dvirtual_op = 10; | |
std::cout << "Assignment: \t" << dvirtual_op.value() << std::endl; | |
return 0; | |
} | |
// Output: | |
// Assignment Operator Testings! | |
// ConcreteOperator | |
// Constructor: 5 | |
// Assignment: 10 | |
// | |
// DConcreteOperator | |
// Constructor: 5 | |
// Assignment: 20 | |
// | |
// VirtualOperator | |
// Constructor: 5 | |
// Assignment: 10 | |
// | |
// DVirtualOperator | |
// Constructor: 10 | |
// Assignment: 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment