C++
Advanced Guide to Learn C++ Programming Effectively
Copyright 2020 - All rights reserved.
The contents of this book may not be reproduced, duplicated or transmitted without direct written permission from the author.
Under no circumstances will any legal responsibility or blame be held against the publisher for any reparation, damages, or monetary loss due to the information herein, either directly or indirectly.
Legal Notice:
This book is copyright protected. This is only for personal use. You cannot amend, distribute, sell, use, quote or paraphrase any part or the content within this book without the consent of the author.
Disclaimer Notice:
Please note the information contained within this document is for educational and entertainment purposes only. Every attempt has been made to provide accurate, up to date and reliable complete information. No warranties of any kind are expressed or implied. Readers acknowledge that the author is not engaging in the rendering of legal, financial, medical or professional advice. The content of this book has been derived from various sources. Please consult a licensed professional before attempting any techniques outlined in this book.
By reading this document, the reader agrees that under no circumstances is the author responsible for any losses, direct or indirect, which are incurred as a result of the use of information contained within this document, including, but not limited to, errors, omissions, or inaccuracies.
Table of Contents
Introduction
I want to thank you for choosing this book, C++ - Advanced Guide to Learn C++ Programming Effectively, and I hope you find the book informative.
If you have read the previous book, you have gathered a basic idea of some concepts in C++ and how you can use loops and conditional statements to address different problems. This, however, does not mean you have mastered the art of programming in C++. You need to have more information to help you write robust programs and applications. This book will shed some light on some advanced topics in C++, which will enhance your understanding of C++.
The book will shed some light on the references and pointers in C++ and their importance. It also provides information on data structures and how you can use them in C++. Since object-oriented programming (OOP) is an important concept in most programming languages, this book sheds some light on what it is and the various concepts in OOP.
In this book, you will learn more about how you can optimize the performance of your code. When you write any code, you need to test it to determine if it runs correctly. You need to find the errors in your code and find a way to overcome those errors. So, what are you waiting for? Grab a copy of this book now and get started. By the end of the book, you will learn how to write code and improve it, so there are no errors and issues when you compile the code.
Chapter One: Using Pointers in C++
Pointers make it easier to perform specific types of tasks in C++. They are easy to use, and it is best to use them to perform activities or tasks, such as dynamic memory allocation. We have looked at the basics of memory allocation in the previous book. This chapter will shed some light on how best you can use pointers in C++.
Every variable you enter into a program or code will be stored in a memory location. Each location has its own address, and these addresses can be accessed in the code using the & operator. This operator denotes that section in the memory where the variable is stored. Let us look at the following example to see how you can print the location or every variable defined in the code.
#include
using namespace std;
int main () {
int var1;
char var2[10];
cout << "Address of var1 variable: ";
cout << &var1 << endl;
cout << "Address of var2 variable: ";
cout << &var2 << endl;
return 0;
}
When you compile the code written above, you obtain the following output:
Address of var1 variable: 0xbfebd5c0
Address of var2 variable: 0xbfebd5b6
The terms 0xbfebd5c0 and 0xbfebd5b6 are the locations in the memory where these variables are stored.
Introduction to Pointers
Before we look at how you can use pointers, let us first understand what a pointer is. Pointers are variables that take the address of a different variable in the code. The syntax of a pointer is as follows:
type *var-name;
The keyword type in the above syntax is the data or base type of the pointer. Make sure the type is a valid data type in C++. The value var-name is the pointers name. You need to use the asterisk in the syntax when you define the pointer. C++ throws an error if you forget to use it. The following are some methods to define pointers.
//The following statements are used to define or declare integer, double, float, and character pointers.
int *ip;
double *dp;
float *fp;
char *ch;
Pointers will only take hexadecimal values since they only take the values of the variables you point them to. You can define a pointer as an integer, double, character, string, etc., but it only represents an address in the memory. The only difference is that when you assign a data type to a pointer when you define it, you indicate to the compiler that you are pointing to a variable with the same data type.
How to Use Pointers in C++
You can perform different operations in C++ using pointers:
Defining a pointer variable
Assigning the pointer with a variable whose address it stores
Accessing the value present in the memory location stored in the pointer
You can perform these operations using the operator * which indicates to the compiler that it needs to return the value of the variable stored at the memory location or address indicated by the pointer. The following example uses these operations:
#include
using namespace std;
int main () {
int var = 20; // actual variable declaration.
int *ip; // pointer variable
ip = &var; // store address of var in pointer variable
cout << "Value of var variable: ";
cout << var << endl;
// print the address stored in ip pointer variable
cout << "Address stored in ip variable: ";
cout << ip << endl;
// access the value at the address available in pointer
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
When you run the code and compile it, you obtain the following output:
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20
Types of Pointers
It is easy to understand how you can use pointers in C++. Having said that, if you make mistakes when you use them in your code, you will receive multiple errors. The following are some concepts to bear in mind when it comes to pointers:
S. No. | Concept | Description |
| Null Pointers | You can use null pointers in C++. This pointer is a constant variable that has a value of zero defined in numerous libraries used in C++. |
| Pointer Arithmetic | You can use the following operators on pointers: - ++
- +
- - -
|
| Pointer vs. arrays |