CPP was derived from C and was simply based on the phenomenon of ‘C with classes’ providing important features such as encapsulation, data abstraction. A CPP program in simple words could also be seen as the collection of objects of one or more class that communicate with each other invoking methods of the respective class. Let now briefly discuss what the above-mentioned term really means
Class: Class in programming languages gives an object-oriented approach to our solution as it not only helps us write code in a better way but also by protecting our data from outside access without permission. In simple language class is a basic template which tells us about the behavior its objects will show.
A class can also be seen as the modification to struct {for those who know C} Though CPP do have struct too but class provides features like data encapsulation which struct does not.
- Methods/functions: A function is basically a set of instruction which we write in our code and whenever we call that function/method compiler to understand that those set of instructions need to be implemented on the data provided at that instant. It helps to avoid repetition of same code again and again, it also helps in providing modularity to the code hence making it much more readable for other co-developers.
- Object: An object is an instance, it may be of a class or struct or any other thing. Object adapts the behavior of its instance like all the variables functions and constants it contains.
- Header Files: Let’s consider you are in a library flooded with millions of books, you are here to solve your doubt on a very advanced mathematical program with millions of books there where would you find the answer? You would just go to the mathematics section and search for advanced mathematics book and solve your problem. Similarly, there are millions of packages/libraries developed for our programming languages so at the top we need to tell the compiler we would be using function (an advanced mathematical problem) from this header file (book?).
Let’s start with the coding itself now:
#include<iostream> //CPP input output stream header file | |
using namespace std; //always end your line by a semicolon(;) | |
int main(){ //main() it’s here where compiler starts running program from | |
cout<<”hello world”; //cout is standard output for cpp | |
return 1; | |
}// Hurray after this small code you can call yourself a coder :p |
Let’s discuss this small but very important code
- The first line "#include<iostream>" it is the header file, whenever you write in CPP you have to at least include this file as it brings in the basic CPP functionality.
- Coming to second line “using namespace std;” it tells the compiler to use std namespace everywhere it was included in latest versions of CPP, also observe the semicolon at the end of it. The semicolon in CPP indicates the end of the line and hence needs to be placed at end of every coding line.
- The next line "int main" it is here where the system starts implementing our code or execution to our code starts.
- cout<< helps in printing any text to the screen.
- The return statement tells system to terminate the main function here.
- "//" these two slashes used are a representation of comments the text after it won’t have any effect on your code compiler would simply ignore this text. Comments are just for the understanding of code and are considered as one of the good practices in coding.
Now you know how to write a basic code in CPP and what each and everything in it is supposed to mean now comes the time for compiling it. Save this file as hello.cpp and run it from the compiler you installed from the local environment topic, after a successful compilation and execution you will be able to see a screen with Hello World written on it on Linux Terminal (with GCC installed) go to directory in which hello.cpp is present then write “gcc hello.cpp” to compile it if it successfully compiles there would be no message, then after no errors present write”./a.out” this price execute the program.
On terminal, it would seem like
@directory$ gcc hello.cpp
@directory$ ./a.out
hello world
Great going so now you have made system execute one of your programs how it feels? But it is just beginning things are easy if you understand them logically rather than memorizing them.
Blocks in CPP:
A block is a set of statements which are linked together or are under one common opening and closing brackets. For example
{ | |
int a; | |
a=10; | |
cout<<a; | |
} |
Here these three lines are under a common pair of opening and closing brackets which tells about interlinking between them, blocks also helps in defining a scope of the variable, we will learn about it more in coming lectures.
CPP identifiers:
CPP identifier represents the name of a variable, class, function, module or any other user-defined item. The identifier can only start with the letter, capital or small, or with the underscore (_) but can contain numbers afterword. Other than Letters, Numbers and Underscore no other character would be considered for naming an Identifier. Also, CPP is a case-sensitive language hence “IdentifierName” and” identifierName” would be considered as two different identifier names.
Examples of some acceptable identifiers:
- abc
- my_name
- Mathematics3
- Class_10th
- a12345
- _abcdef
CPP Reserved words:
CPP have some reserved words i.e., they can’t be used for naming an identifier as those words are already being used by the language.
- asm
- else
- new
- this
- auto
- enum
- operator
- throw
- bool
- explicit
- private
- true
- break
- export
- protected
- try
- case
- extern
- public
- typedef
- catch
- false
- register
- typeid
- char
- float
- reinterpret_cast
- typename
- class
- for
- return
- union
- const
- friend
- short
- unsigned
- const_cast
- goto
- signed
- using
- continue
- if
- sizeof
- virtual
- default
- inline
- static
- void
- delete
- int
- static_cast
- volatile
- do
- long
- struct
- wchar_t
- double
- mutable
- switch
- while
- dynamic_cast
- namespace
- template