0% found this document useful (0 votes)
11 views25 pages

C++ Notes Ut 1

C++ is an object-oriented programming language developed by Bjarne Stroustrup in the 1980s as an extension of the C programming language, incorporating features like classes and objects. It supports multiple programming paradigms, including procedural and generic programming, and is known for its efficiency and flexibility in developing a wide range of applications. Key features of C++ include inheritance, polymorphism, encapsulation, and a rich standard library, making it a powerful tool for software development.

Uploaded by

prabhat.raghav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

C++ Notes Ut 1

C++ is an object-oriented programming language developed by Bjarne Stroustrup in the 1980s as an extension of the C programming language, incorporating features like classes and objects. It supports multiple programming paradigms, including procedural and generic programming, and is known for its efficiency and flexibility in developing a wide range of applications. Key features of C++ include inheritance, polymorphism, encapsulation, and a rich standard library, making it a powerful tool for software development.

Uploaded by

prabhat.raghav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Object Oriented programming Using C++

BCA IIND Year

Unit Ist

C++

C++ is an object-oriented programming language. It is an extension to C programming.

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.

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

Language Year Developed By

Algol 1960 International Group


BCPL 1967 Martin Richard

B 1970 Ken Thompson

Tradition 1972 Dennis Ritchie


al C

K&RC 1978 Kernighan & Dennis Ritchie

C++ 1980 Bjarne Stroustrup

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.

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.

It convert source code directly to machine code.

It has a multi-paradigm programming model.

C++ is an intermediate level /middle-level language programming language, as it


encapsulates both high and low level language features.

C++ is an general purpose programming language:

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.

A Moddle level programming language:

C++ is an intermediate level /middle-level programming language, as it encapsulates


both high and low level language features.

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++, is an compiled language:

Object-Oriented Programming (OOPs)

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

C++ Standard Libraries

Standard C++ programming is divided into three important parts:

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 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.

Let's understand the differences between C and C++.


The following are the differences between C and 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++

1) C follows the procedural style C++ is multi-paradigm. It supports


programming. both 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 C++ supports function overloading.


overloading.
5) In C, you can't use functions in In C++, you can use functions in structure.
structure.

6) C does not support reference C++ supports reference variables.


variables.

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.

8) Operator overloading is not possible Operator overloading is possible in C++.


in C.

9) C programs are divided C++ programs are divided into functions


into procedures and modules and 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 C++ provides exception handling using Try
has to perform using other functions. and Catch block.

12) C does not support the inheritance. C++ supports inheritance.

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.

Let's summarize the above differences in a tabular form.

C++ Features

C++ is a widely used programming language.


It provides a lot of features that are given below.

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.

2) Abstract Data types

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 portable language and programs made in it can be run on different


machines.

4) Mid-level / Intermediate programming language

C++ includes both low-level programming and high-level language so it is known as a


mid-level and intermediate programming language. It is used to develop system
applications such as kernel, driver, etc.

5) Structured programming language

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++ provides very efficient management techniques. The various memory


management operators help save the memory and improve the program's efficiency.
These operators allocate and deallocate memory at run time. Some common memory
management operators available C++ are new, delete etc.
8) Quicker Compilation

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.

13) Compiler based

C++ is a compiler-based programming language, which means no C++ program can


be executed without compilation. C++ compiler is easily available, and it requires
very little space for storage. First, we need to compile our program using a compiler,
and then we can execute our program.

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.

15) National Standards

C++ has national standards such as ANSI.

16) Errors are easily detected

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.

18) Strongly typed language

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.

19) Redefine Existing Operators

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.

20) Modelling real-world problems

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.

Turbo C++ - Download & Installation

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.

1. Download Turbo C++


2. Create turboc directory inside c drive and extract the tc3.zip inside c:\turboc
3. Double click on install.exe file
4. Click on the tc application file located inside c:\TC\BIN to write the c program

1) Download Turbo C++ software

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.

3) Double click on the install.exe file and follow steps

Now, click on the install icon located inside the c:\turboc

66M
1.2K
Difference between JDK, JRE, and JVM

It will ask you to install c or not, press enter to install.


Change your drive to c, press c.

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.

4) Click on the tc application located inside c:\TC\BIN

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.

Now it will showing following console.

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.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.

How to compile and run the C++ program

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 will see the following output on user screen.

You can view the user screen any time by pressing the alt+f5 keys.

Now press Esc to return to the turbo c++ console.

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.

I/O Library Header Files

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.

<fstream It is used to declare services for user-controlled file processing.


>
1.4K
Features of Java - Ja

Standard output stream (cout)

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

Let's see the simple example of standard output stream (cout):

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:

Value of ary is: Welcome to C++ tutorial

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

Standard end line (endl)

The endl is a predefined object of ostream class. It is used to insert a new line
characters and flushes the stream.

Let's see the simple example of standard end line (endl):

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++ Tutorial Javatpoint


End of line

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:Play Videox

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';

Rules for defining variables

A variable can have alphabets, digits and underscore.

A variable name can start with alphabet and underscore only. It can't start with digit.

No white space is allowed within variable name.

A variable name must not be any reserved word or keyword e.g. char, float etc.

Valid variable names:

1. int a;
2. int _ab;
3. int a30;

Invalid variable names:

1. int 4;
2. int x y;
3. int double;

C++ Data Types

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.

Types Data Types

Basic Data Type int, char, float, double, etc

Derived Data Type array, pointer, etc

Enumeration Data Type Enum

User Defined Data Type Structure

Basic Data Types

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.

Data Types Memory Size Range

Char 1 byte -128 to 127

signed char 1 byte -128 to 127


unsigned char 1 byte 0 to 127

Short 2 byte -32,768 to 32,767

signed short 2 byte -32,768 to 32,767

unsigned short 2 byte 0 to 32,767

Int 2 byte -32,768 to 32,767

signed int 2 byte -32,768 to 32,767

unsigned int 2 byte 0 to 32,767

short int 2 byte -32,768 to 32,767

signed short int 2 byte -32,768 to 32,767

unsigned short 2 byte 0 to 32,767


int

long int 4 byte

signed long int 4 byte

unsigned long 4 byte


int

Float 4 byte

Double 8 byte

long double 10 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.

Auto Break Case char const continue default

Double Else Enum extern float for goto

Int Long Register return short signed sizeof

Struct Switch Typedef union unsigned void volatile

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

Delete Mutable protected true try

Typeid Typename using virtual wcha

Catego Ope Associativity


ry rato
r

Postfix () [] Left to right


-> .
++ -
-

Unary + - ! Right to left


~ +
+ - -
(typ
e)*
&
size
of

Multipli */% Left to right


cative
Additiv +- Right to left
e

Shift << Left to right


>>

Relatio < Left to right


nal <=
>
>=

Equalit == ! Right to left


y =/td
>

Bitwise & Left to right


AND

Bitwise ^ Left to right


XOR

Bitwise | Right to left


OR

Logical && Left to right


AND

Logical || Left to right


OR

Conditi ?: Right to left


onal

Assign = Right to left


ment += -
= *=
/=
%=
>>=
<<=
&=
^= |
=

Comm , Left to right


a

Next Topic C++ Operators

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.

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

You might also like