In this tutorial you will learn the basics of C++ and how to use binary and unary operators. This will allow you to write a simple C++ program that you will then compile. The program will be a Visual Studio console application
About this Tutorial –
Objectives –
This course is aimed at students who need to get up to speed in C++. The course introduces object-oriented concepts and shows how they are implemented in C++. The course does not require awareness or familiarity with object-oriented programming techniques, but programming experience would be useful but not necessarily required.
Audience
Students who are new to object orientation (or programming) and need to learn C++.
Prerequisites
No previous experience in C++ programming is required. But any experience you do have in programming will help. Also no experience in Visual Studio is required. But again any experience you do have with programming development environments will be a valuable.
Experience using a contemporary OO language such as Java or C# would be useful but is not required.
Contents
The C++ course covers these topics and more:
Introduction to C++: Key features of C++; Defining variables; Formulating expressions and statements; Built-in data types; Console input/output
Operators and types: Assignment; Compound Assignment; Increment and decrement operators; Const declarations; Type conversions
Going Further with Data Types: Enumerations; Arrays; Using the standard vector class; Using the standard string class; Structures
For built-in types, operators are supported intrinsically
For user-defined types, operators are NOT supported intrinsically // Good
int a, b;
int sum = a + b;
int diff = a - b;
int half = a / 2;
a++;
--b;
if (a == b) ...
if (a >= b) ...
//Bad
Money a, b;
Money sum = a + b;
Money diff = a - b;
Money half = a / 2;
a++;
--b;
if (a == b) ...
if (a >= b) ...
However, you can define operator functions in classes
To add support for operators for your classes
You can support most existing operators (can’t invent new ones!)
Types of Operators
There are two types of operator that you can override…
Number of parambers depends on whether it’s unary/binary
Return type must be consistent with operator for built-in types return_type operator operator_symbol(parameters)
Lab
Defining basic Time and Timespan types
In Visual Studio, create a new C++ project named OperatorOverloadingApp in the student folder.
Define a class named Time as follows:
The class should store a time of day internally as a single integer, i.e. seconds-since-midnight.
Define a constructor to initialize a Time from hours, minutes, and seconds, to allow the client to create objects such as Time(23, 59, 59). Make appropriate use of default arguments, to allow some flexibility here Time::Time(int h, int m, int s)
{
Set(h, m, s);
}
Make appropriate use of default arguments to allow some flexibility here
Add other methods you think might be useful. Keep it simple though! void Time::Set(int h, int m, int s)
{
seconds = h * 3600 + m * 60 + s;
}
int Time::GetHour() const
{
return seconds / 3600;
}
int Time::GetMinute() const
{
int secondsInFullHours = GetHour() * 3600;
int additionalSeconds = seconds - secondsInFullHours;
return additionalSeconds / 60;
}
int Time::GetSecond() const
{
return seconds % 60;
}
string Time::ToString() const
{
stringstream stm;
stm << GetHour() << ":" << GetMinute() << ":" << GetSecond();
return stm.str();
}
Client code can use unary operators as follows: Money money1(10, 20);
Money money2;
// The following two statements are equivalent:
money2 = -money1;
money2 = money1.operator-();
// The following two statements are equivalent:
if (!money1) ...
if (money1.operator!()) ...
A Special Word about ++ and --
The ++ and -- unary operators are special...
They can be used in prefix or postfix position
By definition, this is how you implement ++ (ditto for --): // Header
class Money
{
public:
Money& operator++(); // Prefix
Money operator++(int); // Postfix
...
// Cpp File
Money& Money::operator++()
{
dollars++;
return *this;
}
Money Money::operator++(int)
{
Money orig = *this;
++(*this);
return orig;
}
Lab
Implementing comparison / relational operators
For the Time class, define the following comparison / relational operators:
If you liked this post, please comment with your suggestions to help others.
If you would like to see more content like this in the future, please fill-in our quick survey.
Manage cookie consent
You can view this website without consenting to extra cookies. However you will need to accept the 'marketing' cookies to send messages via the contact forms & see any maps displayed on the site
Functional
Always active
Cookies necessary for the website to work.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.Cookies used to track user interaction on the site, which helps us to evaluate & improve the website.
Marketing: Forms & additional content (Marketing)
We need your permission to place ‘marketing’ cookies, so you are able to use the contact forms & see embedded content e.g. videos and maps. - - - - We have added protection to our contact forms to help prove that a human (rather than a spambot) is filling
If you would like to see more content like this in the future, please fill-in our quick survey.