In this tutorial you will learn the basics of C++ and how to create classes and objects. 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
Data is typically private, to enforce encapsulation
Domain classes
Specific to your business domain
E.g. BankAccount, Customer, Patient, MedicalRecord
Infrastructure classes
Implement technical infrastructure layer
E.g. NetworkConnection, AccountsDataAccess, IPAddress
Exception classes
Represent known types of error
E.g. Exception, BankException, CustomerException
Etc…
What is an Object?
An object is an instance of a class
You can just create an object using Classname objname; syntax
You can also create/destroy dynamically using new/delete keywords (see later)
Object management
Unlike some OO languages (notably Java and C#), C++ doesn’t use garbage-collected
It’s the responsibility of the programmer to ensure objects are disposed of (we’ll come back to this point when we look at new/delete later in the course)
OO Modelling
During OO analysis and design, you map the real world into candidate classes in your application
Use-case modelling
Class diagrams
Sequence diagrams
Object diagrams
State diagrams
UML is the standard OO notation
Widely used
Degree of ceremony varies from one organization to another
Lab
Defining a class
Exercise 1: Defining a class
In Visual Studio, create a new C++ project named DefiningUsingClassesApp in the student folder.
Add a new class named BankAccount. In BankAccount.h, declare the following private data members:
Name of the account holder (a string)
Current balance (a double)
Fees payable on the account (a double) – this allows the bank to make money private:
// State.
string accountHolder;
double currentBalance;
double feesPayable;
int tranAmount;
string tranList[TRAN_SIZE];
};
General syntax for a class declaration (in a .h file)
Similar to a struct template, but with function prototypes added into the mix class ClassName
{
// Define members (data and method prototypes) here.
};
Example: class BankAccount
{
// Define BankAccount data and BankAccount method prototypes here.
};
Access Specifiers
When you define members (i.e. data and methods) in a class, you can use the following access specifiers:
In BankAccount.cpp, implement all the member functions for the BankAccount class BankAccount::BankAccount(const string & name)
{
accountHolder = name;
currentBalance = 0;
feesPayable = 0;
tranAmount = 0;
}
string BankAccount::GetAccountHolder() const
{
return accountHolder;
}
double BankAccount::GetBalance() const
{
return currentBalance;
}
double BankAccount::GetFeesPayable() const
{
return feesPayable;
}
// Try and add a AddTransaction yourself, the solution is always available to download from the top of the tutorial
Note that the deposit and withdraw methods should levy a fee against the bank account – the user has to pay a certain fee every time they deposit or withdraw. Harsh yes, but hey, this is the Dawning of the Age of Austerity void BankAccount::Deposit(double amount)
{
feesPayable += FEE_PAYABLE;
currentBalance += amount;
AddTransaction(amount, currentBalance, "[D]");
}
// Withdraw Function
void BankAccount::Withdraw(double amount)
{
if (amount > currentBalance)
{
cout << "Insufficient funds!" << endl;
}
else
{
feesPayable += FEE_PAYABLE;
currentBalance -= amount;
AddTransaction(amount, currentBalance, "[W]");
}
}
Creates the object on the "stack", local to this function
Automatically popped off the stack (i.e. de-allocated) on exit from the function void someFunctionInMyCode()
{
// Create a BankAccount object, on the stack.
// Note, unlike Java and C#, you don't have to use the new keyword.
// (Although it is possible to use new, we'll come back to that later...)
BankAccount myAccount;
// Use object here...
} // Object automatically disappears here.
Called automatically when you create an object with no parameters // Header
class BankAccount
{
...
public:
BankAccount();
...
};
// Cpp file
#include "BankAccount.h"
BankAccount::BankAccount()
{
accountHolder = "Unknown";
id = -1;
balance = 0;
}
Defining Constructors with Parameters
You can also define constructor(s) that take parameters
Called when you create an object with parameters // Header File
class BankAccount
{
...
public:
BankAccount(string ah, int id, double initBal);
...
};
// Cpp File
#include "BankAccount.h"
BankAccount::BankAccount(string ah, int id, double initBal)
{
this->accountHolder = ah;
this->id = id;
this->balance = initBal;
}
// Client
// Create an object
BankAccount acc2("John Smith", 1, 1000);
Well done. You have completed the tutorial in the C++ course. The next tutorial is
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.