Chap1 Intro To OOP C++
Chap1 Intro To OOP C++
Chap1 Intro To OOP C++
What is C++
C++ is a general purpose, case-sensitive, free-form programming language that supports object-oriented,
procedural and generic programming. C++ is a middle-level language, as it encapsulates both high and
low level language features.
Usage of C++
By the help of C++ programming language, we can develop different types of secured and robust
applications:
Window application
Client-Server application
Device drivers
Embedded firmware etc
C++ Program
In this tutorial, all C++ programs are given with C++ compiler so that you can easily change the C++
program code.
File: main.cpp
1. #include <iostream>
2. using namespace std;
3. int main() {
1|Page
4. cout << "Hello C++ Programming";
5. return 0;
6. }
C vs. C++
What is C?
C is a structural or procedural oriented programming language which is machine-independent and
extensively used in various applications.
C is the basic programming language that can be used to develop from the operating systems (like
Windows) to complex programs like Oracle database, Git, Python interpreter, and many more. C
programming language can be called a god's programming language as it forms the base for other
programming languages. If we know the C language, then we can easily learn other programming
languages. C language was developed by the great computer scientist Dennis Ritchie at the Bell
Laboratories. It contains some additional features that make it unique from other programming
languages.
What is C++?
C++ is a special-purpose programming language developed by Bjarne Stroustrup at Bell Labs circa
1980. C++ language is very similar to C language, and it is so compatible with C that it can run 99% of
C programs without changing any source of code though C++ is an object-oriented programming
language, so it is safer and well-structured programming language than C.
3|Page
Let's summarize the above differences in a tabular form.
N C C++
o.
1) C follows the procedural style C++ is multi-paradigm. It supports both
programming. procedural and object oriented.
2) Data is less secured in C. In C++, you can use modifiers for class members
to make it inaccessible for outside users.
3) C follows the top-down approach. C++ follows the bottom-up approach.
4) C does not support function overloading. C++ supports function overloading.
5) In C, you can't use functions in structure. In C++, you can use functions in structure.
6) C does not support reference variables. C++ supports reference variables.
7) In C, scanf() and printf() are mainly used C++ mainly uses stream cin and cout to perform
for input/output. input and output operations.
8) Operator overloading is not possible in C. Operator overloading is possible in C++.
9) C programs are divided into procedures C++ programs are divided into functions and
and modules classes.
10 C does not provide the feature of C++ supports the feature of namespace.
) namespace.
11 Exception handling is not easy in C. It has C++ provides exception handling using Try and
) to perform using other functions. Catch block.
12 C does not support the inheritance. C++ supports inheritance.
)
C++ history
History of C++ language is interesting to know. Here we are going to discuss brief history of C++
language.
C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of
AT&T (American Telephone & Telegraph), located in U.S.A.
Bjarne Stroustrup is known as the founder of C++ language.
It was develop for adding a feature of OOP (Object Oriented Programming) in C without
significantly changing the C component.
C++ programming is "relative" (called a superset) of C, it means any valid C program is also a valid
C++ program.
Let's see the programming languages that were developed before C++ language.
Language Year Developed By
Algol 1960 International Group
BCPL 1967 Martin Richard
B 1970 Ken Thompson
4|Page
Traditional C 1972 Dennis Ritchie
K&RC 1978 Kernighan & Dennis
Ritchie
C++ 1980 Bjarne Stroustrup
C++ Features
C++ is object oriented programming language. It provides a lot of features that are given below.
1) Simple
C++ is a simple language in the sense that it provides structured approach (to break the problem into
parts), rich set of library functions, data types etc.
5) Rich Library
C++ provides a lot of inbuilt functions that makes the development fast.
6) Memory Management
It supports the feature of dynamic memory allocation. In C++ language, we can free the allocated
memory at any time by calling the free() function.
7) Speed
The compilation and execution time of C++ language is fast.
8) Pointer
C++ provides the feature of pointers. We can directly interact with the memory by using the pointers.
We can use pointers for memory, structures, functions, array etc.
5|Page
9) Recursion
In C++, we can call the function within the function. It provides code reusability for every function.
10) Extensible
C++ language is extensible because it can easily adopt new features.
C++ Program
Before starting the abcd of C++ language, you need to learn how to write, compile and run the first
C++ program.
To write the first C++ program, open the C++ console and write the following code:
1. #include <iostream.h>
2. #include<conio.h>
3. void main() {
4. clrscr();
5. cout << "Welcome to C++ Programming.";
6. getch();
7. }
#include<iostream.h> includes the standard input output library functions. It provides cin and cout
methods for reading from input and writing to output respectively.
#include <conio.h> includes the console input output library functions. The getch() function is
defined in conio.h file.
void main() The main() function is the entry point of every program in C++ language. The void
keyword specifies that it returns no value.
cout << "Welcome to C++ Programming." is used to print the data "Welcome to C++
Programming." on the console.
getch() The getch() function asks for a single character. Until you press any key, it blocks the screen.
6|Page
C++ Basic Input/Output
C++ I/O operation is using the stream concept. Stream is the sequence of bytes or flow of data. It
makes the performance fast.
If bytes flow from main memory to device like printer, display screen, or a network connection, etc,
this is called as output operation.
If bytes flow from device like printer, display screen, or a network connection, etc to main memory,
this is called as input operation.
7|Page
Standard input stream (cin)
The cin is a predefined object of istream class. It is connected with the standard input device, which is
usually a keyboard. The cin is used in conjunction with stream extraction operator (>>) to read the input
from a console.
Let's see the simple example of standard input stream (cin):
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. int age;
5. cout << "Enter your age: ";
6. cin >> age;
7. cout << "Your age is: " << age << endl;
8. }
Output:
Enter your age: 22
Your age is: 22
8|Page
C++ Variable
A variable is a name of memory location. It is used to store data. Its value can be changed and it can be
reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:
1. type variable_list;
The example of declaring variable is given below:
1. int x;
2. float y;
3. char z;
Here, x, y, z are variables and int, float, char are data types.
We can also provide values while declaring the variables as given below:
1. int x=5,b=10; //declaring 2 variable of integer type
2. float f=30.8;
3. char c='A';
9|Page
C++ Data Types
A data type specifies the type of data that a variable can store such as integer, floating, character etc.
The memory size of basic data types may change according to 32 or 64 bit operating system.
Let's see the basic data types. It size is given according to 32 bit OS.
A list of 30 Keywords in C++ Language which are not available in C language are given below.
asm dynamic_cast namespace reinterpret_cast bool
explicit new static_cast false catch
operator template friend private class
this inline public throw const_cast
delete mutable protected true try
typeid typename using virtual wchar_t
C++ Operators
An operator is simply a symbol that is used to perform operations. There can be many types of
operations like arithmetic, logical, bitwise etc.
There are following types of operators to perform different types of operations in C language.
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operator
Unary operator
Ternary or Conditional Operator
Misc Operator
11 | P a g e
Precedence of Operators in C++
The precedence of operator species that which operator will be evaluated first and next. The
associativity specifies the operators direction to be evaluated, it may be left to right or right to left.
C++ Identifiers
C++ identifiers in a program are used to refer to the name of the variables, functions, arrays, or other
user-defined data types created by the programmer. They are the basic requirement of any language.
Every language has its own rules for naming the identifiers.
In short, we can say that the C++ identifiers represent the essential elements in a program which are
given below:
Constants
Variables
Functions
Labels
Defined data types
Some naming rules are common in both C and C++. They are as follows:
Valid Identifiers
The following are the examples of valid identifiers are:
1. Result
2. Test2
3. _sum
4. power
13 | P a g e
Invalid Identifiers
The following are the examples of invalid identifiers:
1. Sum-1 // containing special character '-'.
2. 2data // the first letter is a digit.
3. break // use of a keyword.
Note: Identifiers cannot be used as the keywords. It may not conflict with the keywords, but it is
highly recommended that the keywords should not be used as the identifier name. You should
always use a consistent way to name the identifiers so that your code will be more readable and
maintainable.
The major difference between C and C++ is the limit on the length of the name of the variable. ANSI C
considers only the first 32 characters in a name while ANSI C++ imposes no limit on the length of the
name.
Constants are the identifiers that refer to the fixed value, which do not change during the execution of a
program. Both C and C++ support various kinds of literal constants, and they do have any memory
location. For example, 123, 12.34, 037, 0X2, etc. are the literal constants.
Let's look at a simple example to understand the concept of identifiers.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a;
6. int A;
7. cout<<"Enter the values of 'a' and 'A'";
8. cin>>a;
9. cin>>A;
10. cout<<"\nThe values that you have entered are : "<<a<<" , "<<A;
11. return 0;
12. }
In the above code, we declare two variables 'a' and 'A'. Both the letters are same but they will behave as
different identifiers. As we know that the identifiers are the case-sensitive so both the identifiers will
have different memory locations.
Output
14 | P a g e
What are the keywords?
Keywords are the reserved words that have a special meaning to the compiler. They are reserved for a
special purpose, which cannot be used as the identifiers. For example, 'for', 'break', 'while', 'if', 'else',
etc. are the predefined words where predefined words are those words whose meaning is already
known by the compiler. Whereas, the identifiers are the names which are defined by the programmer to
the program elements such as variables, functions, arrays, objects, classes.
Differences between Identifiers and Keywords
The following is the list of differences between identifiers and keywords:
Identifiers Keywords
Identifiers are the names defined by the programmer Keywords are the reserved words whose
to the basic elements of a program. meaning is known by the compiler.
It is used to identify the name of the variable. It is used to specify the type of entity.
It can consist of letters, digits, and underscore. It contains only letters.
It can use both lowercase and uppercase letters. It uses only lowercase letters.
No special character can be used except the It cannot contain any special character.
underscore.
The starting letter of identifiers can be lowercase, It can be started only with the lowercase
uppercase or underscore. letter.
It can be classified as internal and external It cannot be further classified.
identifiers.
Examples are test, result, sum, power, etc. Examples are 'for', 'if', 'else', 'break', etc.
15 | P a g e