In this tutorial you will learn the basics of C++ and how to use structures. 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
Only use C-style strings when interfacing with legacy C systems
To use the C++ string class
#include <string> and use the std namespace
Using the C++ string class
Here are some examples that illustrate some capabilities of the C++ string class // Assignment or initialization.
string s1 = "hello";
string s2 = " world;
string message = s1;
// Concatenation.
message += s2;
message += ", have a nice day";
// Comparison.
if (message == "hello world, have a nice day")
{
// Indexing.
message[0] = 'H';
}
For full information:
See https://www.cplusplus.com/reference/string/string/
Lab
Defining a struct and using it
In Visual Studio, create a new C++ project named GoingFurtherWithTypesApp in the student folder. Create a header file named Footballer.h. In this header file, define a structure template named Footballer, to hold:
The name of the footballer
His/her squad number
His/her position (e.g. GOALKEEPER, DEFENDER, MIDFIELDER, STRIKER
His/her goals scored (use a typedef for this type, e.g. to represent unsigned short) enum Position
{
GOALKEEPER, DEFENDER, MIDFIELDER, STRIKER
};
typedef unsigned short goals_t;
struct Footballer
{
string name;
int squadNumber;
Position positionCode;
goals_t goalsScored;
};
Determine if a footballer is any good (goalsScored >= 10, bit harsh on the goalie...) bool IsGood(const Footballer *pFootballer)
{
return pFootballer->goalsScored >= 10;
}
Typically defined in a header file enum enum_typename { pneumonic1, pneumonic2, pneumonic3, ... };
Examples:
By default, values start at 0 and increase in steps of 1 enum Season { Spring, Summer, Autumn, Winter };
You can specify different values if you like enum Month { Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Err=-1 };
Usage in client code: Season myFavouriteSeason = summer;
Month myMonthOfBirth = Dec;
Lab
Using arrays of structures
In main(), create an array of footballers. Call the array "team". There are 11 players in a real football team, but you can use a smaller number for testing : void InputTeam(Footballer team[], int numPlayers)
{
cout << endl << "Enter details for " << numPlayers << " players: " << endl;
for (int i = 0; i < numPlayers; i++)
{
InputFootballer(&team[i]);
}
}
void DisplayTeam(const Footballer team[], int numPlayers)
{
cout << endl << "Here are the details for " << numPlayers << " players: " << endl;
for (int i = 0; i < numPlayers; i++)
{
DisplayFootballer(&team[i]);
}
}
Using the functions you've already written, initialize and display the footballer details Footballer team[5]; // 5-a-side.
InputTeam(team, 5);
DisplayTeam(team, 5);
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.