C++ Notes Ut 1
C++ Notes Ut 1
Unit Ist
C++
In 1970’s C one of the most used languages but C has some limitations & lot of complexity increased so it’s
not capable to handle all problems. Developers add object-oriented programming concept with C and
introduced it with the name “c with classes”.
C++ was introduced by BJARNE STROUSTRUP in 1979 with the name “C WITH CLASSES”.
C++ was renamed in 1983 with the name C++, Plus Plus ++ is increment operator and incrementation in C.
C++ history
History of C++ language is interesting to know. Here we are going to discuss brief
history of C++ language.
It was develop for adding a feature of OOP (Object Oriented Programming) in C without
significantly changing the C component
C++ program.
Let's see the programming languages that were developed before C++ language.
Data Types, control statements, functions, arrays, strings, constructor, destructor, this, static, namespace,
objects and classes, inheritance, polymorphism, abstraction, abstract class, interface, encapsulation,
exception handling, File IO, etc.
What is C++
C++ is a general purpose, pre compiled and free-form programming language that
supports object-oriented, procedural and generic programming.
A general purpose programming language that designed to be used for writing the
software in a wide varity of application domains. We can use a general purpose
programming language to create different varity of application.
Low Level: directly interacts with machine code & machine language and assembly
language are the example of these type of language.
High Language: in this a programmer easily write the program in a language, which
almost similer to user understandable language or human readable formate..
C++ supports the object-oriented programming, the four major pillar of object-
oriented programming (OOPs) used in C++ are:
1. Inheritance
2. Polymorphism
3. Encapsulation
4. Abstraction
o The core library includes the data types, variables and literals, etc.
o The standard library includes the set of functions manipulating strings, files, etc.
o The Standard Template Library (STL) includes the set of methods manipulating
a data structure.
Usage of C++
By the help of C++ programming language, we can develop different types of secured
and robust applications:
o Window application
o Client-Server application
o Device drivers
o 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() {
4. cout << "Hello C++ Programming";
5. return 0;
6. }
C vs. C++
What is C?
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++?
o Definition
C is a structural programming language, and it does not support classes and
objects, while C++ is an object-oriented programming language that supports
the concept of classes and objects.
o Type of programming language
C supports the structural programming language where the code is checked line
by line, while C++ is an object-oriented programming language that supports
the concept of classes and objects.
o Developer of the language
Dennis Ritchie developed C language at Bell Laboratories while Bjarne
Stroustrup developed the C++ language at Bell Labs circa 1980.
o Subset
C++ is a superset of C programming language. C++ can run 99% of C code but
C language cannot run C++ code.
o Type of approach
C follows the top-down approach, while C++ follows the bottom-up approach.
The top-down approach breaks the main modules into tasks; these tasks are
broken into sub-tasks, and so on. The bottom-down approach develops the
lower level modules first and then the next level modules.
o Security
In C, the data can be easily manipulated by the outsiders as it does not support
the encapsulation and information hiding while C++ is a very secure language,
i.e., no outsiders can manipulate its data as it supports both encapsulation and
data hiding. In C language, functions and data are the free entities, and in C++
language, all the functions and data are encapsulated in the form of objects.
o Function Overloading
Function overloading is a feature that allows you to have more than one
function with the same name but varies in the parameters. C does not support
the function overloading, while C++ supports the function overloading.
o Function Overriding
Function overriding is a feature that provides the specific implementation to the
function, which is already defined in the base class. C does not support the
function overriding, while C++ supports the function overriding.
o Reference variables
C does not support the reference variables, while C++ supports the reference
variables.
o Keywords
C contains 32 keywords, and C++ supports 52 keywords.
o Namespace feature
A namespace is a feature that groups the entities like classes, objects, and
functions under some specific name. C does not contain the namespace feature,
while C++ supports the namespace feature that avoids the name collisions.
o Exception handling
C does not provide direct support to the exception handling; it needs to use
functions that support exception handling. C++ provides direct support to
exception handling by using a try-catch block.
o Input/Output functions
In C, scanf and printf functions are used for input and output operations,
respectively, while in C++, cin and cout are used for input and output
operations, respectively.
o Memory allocation and de-allocation
C supports calloc() and malloc() functions for the memory allocation, and free()
function for the memory de-allocation. C++ supports a new operator for the
memory allocation and delete operator for the memory de-allocation.
No. C C++
2) Data is less secured in C. In C++, you can use modifiers for class
members to make it inaccessible for outside
users.
7) In C, scanf() and printf() are mainly C++ mainly uses stream cin and cout to
used for input/output. perform input and output operations.
10) C does not provide the feature of C++ supports the feature of namespace.
namespace.
11) Exception handling is not easy in C. It C++ provides exception handling using Try
has to perform using other functions. and Catch block.
o Inheritance
Inheritance is a feature that allows the child class to reuse the properties of the
parent class. C language does not support the inheritance while C++ supports
the inheritance.
o Header file
C program uses <stdio.h> header file while C++ program
uses <iostream.h> header file.
C++ Features
1. Simple
2. Abstract Data types
3. Machine Independent or Portable
4. Mid-level programming language
5. Structured programming language
6. Rich Library
7. Memory Management
8. Quicker Compilation
9. Pointers
10. Recursion
11. Extensible
12. Object-Oriented
13. Compiler based
14. Reusability
15. National Standards
16. Errors are easily detected
17. Power and Flexibility
18. Strongly typed language
19. Redefine Existing Operators
20. Modeling Real-World Problems
21. Clarity
1) Simple
C++ is a simple language because it provides a structured approach (to break the
problem into parts), a rich set of library functions, data types, etc.
In C++, complex data types called Abstract Data Types (ADT) can be created using
classes.
64.6M
1.2K
C++ vs Java
3) Portable
C++ is a structured programming language. In this we can divide the program into
several parts using functions.
6) Rich Library
C++ provides a lot of inbuilt functions that make the development fast. Following are
the libraries used in C++ programming are:
o <iostream>
o <cmath>
o <cstdlib>
o <fstream>
7) Memory Management
C++ programs tend to be compact and run quickly. Hence the compilation and
execution time of the C++ language is fast.
9) Pointer
C++ provides the feature of pointers. We can use pointers for memory, structures,
functions, array, etc. We can directly interact with the memory by using the pointers.
10) Recursion
In C++, we can call the function within the function. It provides code reusability for
every function.
11) Extensible
C++ programs can easily be extended as it is very easy to add new features into the
existing program.
12) Object-Oriented
In C++, object-oriented concepts like data hiding, encapsulation, and data abstraction
can easily be implemented using keyword class, private, public, and protected access
specifiers. Object-oriented makes development and maintenance easier.
14) Reusability
With the use of inheritance of functions programs written in C++ can be reused in any
other program of C++. You can save program parts into library files and invoke them
in your next programming projects simply by including the library files. New programs
can be developed in lesser time as the existing code can be reused. It is also possible
to define several functions with same name that perform different task. For Example:
abs () is used to calculate the absolute value of integer, float and long integer.
It is easier to maintain a C++ programs as errors can be easily located and rectified.
It also provides a feature called exception handling to support error handling in your
program.
17) Power and Flexibility
C++ is a powerful and flexible language because of most of the powerful flexible and
modern UNIX operating system is written in C++. Many compilers and interpreters for
other languages such as FORTRAN, PERL, Python, PASCAL, BASIC, LISP, etc., have
been written in C++. C++ programs have been used for solving physics and
engineering problems and even for animated special effects for movies.
The list of arguments of every function call is typed checked during compilation. If
there is a type mismatch between actual and formal arguments, implicit conversion is
applied if possible. A compile-time occurs if an implicit conversion is not possible or if
the number of arguments is incorrect.
C++ allows the programmer to redefine the meaning of existing operators such as +,
-. For Example, The "+" operator can be used for adding two numbers and
concatenating two strings.
The programs written in C++ are well suited for real-world modeling problems as
close as possible to the user perspective.
21) Clarity
The keywords and library functions used in C++ resemble common English words.
There are many compilers available for C++. You need to download any one. Here, we
are going to use Turbo C++. It will work for both C and C++. To install the Turbo C+
+ software, you need to follow following steps.
You can download turbo C++ from many sites. download Turbo c++
2) Create turboc directory in c drive and extract the tc3.zip
Now, you need to create a new directory turboc inside the c: drive. Now extract the
tc3.zip file in c:\turboc directory.
66M
1.2K
Difference between JDK, JRE, and JVM
Press enter, it will look inside the c:\turboc directory for the required files.
Select Start installation by the down arrow key then press enter.
Now C is installed, press enter to read documentation or close the software.
Now double click on the tc icon located in c:\TC\BIN directory to write the c program.
In windows 7 or window 8, it will show a dialog block to ignore and close the
application because full screen mode is not supported. Click on Ignore button.
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 <conio.h> includes the console input output library functions. The
getch() function is defined in conio.h file.lay Videox
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.
There are 2 ways to compile and run the C++ program, by menu and by shortcut.
By menu
Now click on the compile menu then compile sub menu to compile the c++
program.
Then click on the run menu then run sub menu to run the c++ program.
By shortcut
Or, press ctrl+f9 keys compile and run the program directly.
You can view the user screen any time by pressing the alt+f5 keys.
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.
Let us see the common header files used in C++ programming are:
66M
Header File Function and
Description
<iostrea It is used to define the cout, cin and cerr objects, which correspond to
m> standard output stream, standard input stream and standard error
stream, respectively.
<iomanip It is used to declare services useful for performing formatted I/O, such
> as setprecision and setw.
The cout is a predefined object of ostream class. It is connected with the standard
output device, which is usually a display screen. The cout is used in conjunction with
stream insertion operator (<<) to display the output on a console
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. char ary[] = "Welcome to C++ tutorial";
5. cout << "Value of ary is: " << ary << endl;
6. }
Output:
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.
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:
The endl is a predefined object of ostream class. It is used to insert a new line
characters and flushes the stream.
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. cout << "C++ Tutorial";
5. cout << " Javatpoint"<<endl;
6. cout << "End of line"<<endl;
7. }
Output:
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.
1. type variable_list;
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:
A variable name can start with alphabet and underscore only. It can't start with digit.
A variable name must not be any reserved word or keyword e.g. char, float etc.
1. int a;
2. int _ab;
3. int a30;
1. int 4;
2. int x y;
3. int double;
A data type specifies the type of data that a variable can store such as integer,
floating, character etc.
There are 4 types of data types in C++ language.
The basic data types are integer-based and floating-point based. C++ language
supports both signed and unsigned literals.
The memory size of basic data types may change according to 32 or 64 bit operating
system.
Float 4 byte
Double 8 byte
Let's see the basic data types. It size is given according to 32 bit OS
C++ Keywords
A keyword is a reserved word. You cannot use it as a variable name, constant name
etc. A list of 32 Keywords in C++ Language which are also available in C
language are given below.
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.
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operator
o Unary operator
o Ternary or Conditional Operator
o Misc Operator