Share
C++ Programming

Course Outline

Week 1

INTRODUCTION TO C++ PROGRAMMING

  • Differences Between C and C++ Programming
  • Compiler 
  • C++ IDE
  • Creating and running C++ Program
  • Variables
  • Scope of variables
Week 2
Week 3
Week 4

SponsoredAdvertise

Introduction to c++ programming

Introduction To C++

 

C++ is a cross-platformed language that can be used to create sophisticated high-performance applications.

 

C++ was developed in Bell labs 1979 as an extension to its predecessor, the C language.

 

C++ gives programmers a high level of control over system resources and memory.

 

The language was updated 3 major times in 2011, 2014,and 2017 to C++11, C++14, and C++17.

 

Differences Between C and C++ Programming

 

C C++
C was developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs. C++ was developed by Bjarne Stroustrup in 1979 with C++'s predecessor "C with Classes".
When compared to C++, C is a subset of C++. C++ is a superset of C. C++ can run most of C code while C cannot run C++ code.
C supports procedural programming paradigm for code development. C++ supports both procedural and object oriented programming paradigms; therefore C++ is also called a hybrid language.
C does not support object oriented programming; therefore it has no support for polymorphism, encapsulation, and inheritance. Being an object oriented programming language C++ supports polymorphism, encapsulation, and inheritance.
In C (because it is a procedural programming language), data and functions are separate and free entities. In C++ (when it is used as object oriented programming language), data and functions are encapsulated together in form of an object. For creating objects class provides a blueprint of structure of the object.
In C, data are free entities and can be manipulated by outside code. This is because C does not support information hiding. In C++, Encapsulation hides the data to ensure that data structures and operators are used as intended.
C, being a procedural programming, it is a function driven language. While, C++, being an object oriented programming, it is an object driven language.
C does not support function and operator overloading. C++ supports both function and operator overloading.
C does not allow functions to be defined inside structures. In C++, functions can be used inside a structure.
C does not have namespace feature. C++ uses NAMESPACE which avoid name collisions.
C uses functions for input/output. For example scanf and printf. C++ uses objects for input output. For example cin and cout.
C does not support reference variables. C++ supports reference variables.
C has no support for virtual and friend functions. C++ supports virtual and friend functions.
C provides malloc() and calloc() functions for dynamic memory allocation, and free() for memory de-allocation. C++ provides new operator for memory allocation and delete operator for memory de-allocation.
C does not provide direct support for error handling (also called exception handling) C++ provides support for exception handling. Exceptions are used for "hard" errors that make the code incorrect.

 

Uses Of C++


Applications


C++ is used for the development of new applications of C++. Many applications of Adobe Systems are developed in C++ like Illustrator, adobe premiere and image ready


Games


C++ is used for developing games. It supports multiplayer option with networking. It is also used in developing the suites of a game tool.


Animations


There are animated software which are developed with the help of C++ language. 3D animation, modeling, simulation and rendering software.


Web Browser


 C++ is used for making Google Chrome and Mozilla Internet Browser Firefox.


Database Access


C++ is used for developing database software or open source database software. An example is MySQL which is the most popular database management system


Media Access


C++ is also used for creating media player, managing video files and audio files. An example is winamp media player which is developed in C++ language


Operating Systems


C++ is also used for developing most of the operating systems for Microsoft and few parts of Apple Operating System.

 

Advantages of C++

  • Is a highly portable language and is often the language of selection for multi-device, multi-platform app development
  • Is an object-oriented programming and includes concepts like classes, inheritance, polymorphism, data abstraction and encapsulation which allow code reusability and makes programs maintainable
  • It gives the user complete control over memory management
  • The wide range of applications: from GUI applications to 3D graphics for games to real-time mathematical simulations, C++ is everywhere


C++ Compilers

A compiler is a special program that processes statement written in a particular programming language and turns them into machine language (“code” made up of 0’s and 1’s that computer’s processor uses).


Image


Figure: Compiler illustration


Creating Executable C++ File

 

The easiest way to compile C++ programs is by using an Integrated Development Environment (IDE). An IDE integrates several development tools, including a text editor and tools to compile programs directly from it.

 

IDE for Compiling C++ Programs


  • Code::blocks
  • Visual Studio Express
  • Dev – C++


We will be using Code:blocks as our IDEfor this course.

Download Code::blocks at http://www.codeblocks.org/downloads/binaries


Get the one with the mingw-setup in the executable file name. That one comes with both the IDE and the compiler so you won't have to install another plugin for the compiler.


CREATING YOUR FIRST C++ PROGRAM


Start the CodeBlock



Figure: CodeBlock


To create a project, click on File -> New -> Project


 

Figure: New Project creation


Select the project type. 


We will be developing console applications in this course. So select the Console Application option.


 

Figure: Console Application


Click Next and select C++




Fill the Project Title, browse to where to save your project 


 

Figure: Project Creation Form 


 

 

Figure: Project Name Details


Click on next



 Figure: Compiler Selection Form


Click on Finish


Image


Figure: main.cpp file to be edited


Variables


A variable provides name storage that programs can manipulate.

 

The name of a variable can be composed of letters,digits, and the underscore character. It must begin with either a letter or an underscore.Upper and lowercase letters are distinct because C++ is case-sensitive

 

Variable Types

 

Type & Description
bool  
Stores either value true or false.
char
Typically a single octet (one byte). This is an integer type.
int
The most natural size of integer for the machine.
float
A single-precision floating point value.
double
A double-precision floating point value.
void
Represents the absence of type.

 

Variable Definition

 

Variables are declared in the following formats.

 

Variabletype variable name;

 

Thus you enter the variable type and leave a space then type the name you want to assign to the variable and end thecommand with a semi column (;)

 

NOTE: ; is placed at the end of the programming command.

 

Examples of Variable Definition

 

int a;

int b;

int c;

int d;

You can also declare more than one variable at once by typing a comma after the previous variable name before typing the next variable name.

Thus the above variable declarations could be written as

int a,b,c,d;

More Examples

int    i, j, k;

char   c, ch;

float  f, salary;

double d;

 

Variable Scope


A scope is a region of the program. There are three places where variables can be declared.

  • Inside a function or a block which is called local variables
  • In the definition of function parameters which is called formal parameters
  • Outside of all functions which is called global variables


Local Variables

 

Local Variables are declared inside a function or block. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own.

 

Example


#include <iostream>
using namespace std;
int main () {
   // Local variable declaration:
     int a, b;
      int c;
     return 0;
}

 

Global Variables

 

Global variables are defined outside of all the functions, usually on top of the program. A global variable can be accessed by any function. A global variable is available for use throughout the entire program after its declaration.

 

#include <iostream>
using namespace std;
 
// Global variable declaration:
int g;
 
int main () {
   // Local variable declaration:
   int a, b;
 
   // actual initialization
   a = 10;
   b = 20;
   g = a + b;
  
   cout << g;
 
   return 0;
}

 

A program can have same name for local and global variables but value of local variable inside a function will take preference.


#include <iostream>
using namespace std;
 
// Global variable declaration:
int c = 5;
 
int main () {
   // Local variable declaration:
   int c = 2;
 
   cout << c;
 
   return 0;
}

 

The above program output will be 2 because the local variable declared (int c = 2)declared in the function will take preference.

Function Parameters


This will be elaborated more under the topic functions. 


Image


Assignment 1

 

State a programming application that you will like to create and the possible variable names and their types to use. Write their declarations. Example

 

An application to register students

 

Variable Name Variable Type Declaration
firstname char char firstname;
lastname char char lastname;
age int Int age;

 

 

 

SponsoredAdvertise