Sunteți pe pagina 1din 23

Introduction:

C++ was invented in 1979 by Bjarne Stroustrup at Bell Laboratories in New Jersey.C++ is a super set of ‘C’

language.Initially it was called “C with Class”.The name was changed to C++ in 1983.

SOFTWARE EVOLUTION

*The software evolution has different phases or layers.

*Each layer representing an improvement over the previous layer.

*The machine language(0,1) are only understood by the machine.This is low-level language.

*Assemble language is a middle level language.It can be understand by people and machine.

*Procedure oriented language used to solve mathematical problems.

*Object-oriented programming used to solve mathematical problems and real world problems.

A look at procedure C++(object oriented program)

*The high level languages such as COBOL,FORTRAN and C are commonly known as procedure oriented languages.

*In the procedure oriented programming every problem can be solved as a sequence of instruction,such as

reading,calculating and printing the result.

*Flowchart can be used to organize actions and flow of control.

*When the program becomes larger,we use the principle of dividing a program into smaller pieces called a procedure

or function.

*In a multifunction program,many important data items are places global so that may be accessed by all the

function.Each function may have its own local data.

Relationship between data & function in POP


CHARACTERISTIC OF PROCEDURE –ORIENTED LANGUAGE:

*Emphasis is on doing things(algorithms).

*Large programs are divided into smaller programs known as function.

*Most of the function share global data.

*Date move openly around the system function to function.

*Function transforms data from one form function to another.

*Program design is top-to-down approach.

C++ OOP's

object oriented program paradigm

A programming paradigm is a style, or “way,” of programming. The word “paradigm” is used a


great deal when talking about programming languages.

*The fundamental idea behind OOP is to combine both data and functions that operate on that data into a single

unit.Such a unit is called an object.


*A OOP program consists of a number of objects.The data of an object can be accessed by the function associated with

that objects.

Features of OOP:-

*Emphasis is on data rather than procedure.

*Programs are divided into as objects.

*Data is hidden and cannot be accessed by the external function.

*Object may communicate with each other through function.

*New data and functions can be easily added whenever necessary.

*Program design is bottom-to-top approach.

Some Common Paradigms


You should know these:

 Imperative: Programming with an explicit sequence of commands that update state.

 Declarative: Programming by specifying the result you want, not how to get it.

 Structured: Programming with clean, goto-free, nested control structures.

 Procedural: Imperative programming with procedure calls.

 Functional (Applicative): Programming with function calls that avoid any global state.

 Function-Level (Combinator): Programming with no variables at all.


 Object-Oriented: Programming by defining objects that send messages to each other.
Objects have their own internal (encapsulated) state and public interfaces. Object
orientation can be:

o Class-based: Objects get state and behavior based on membership in a class.

o Prototype-based: Objects get behavior from a prototype object.

 Event-Driven: Programming with emitters and listeners of asynchronous actions.

 Flow-Driven: Programming processes communicating with each other over predefined


channels.

 Logic (Rule-based): Programming by specifying a set of facts and rules. An engine


infers the answers to questions.

 Constraint: Programming by specifying a set of constraints. An engine finds the values


that meet the constraints.

 Aspect-Oriented: Programming cross-cutting concerns applied transparently.

 Reflective: Programming by manipulating the program elements themselves.

 Array: Programming with powerful array operators that usually make loops
unnecessary.

Paradigms are not meant to be mutually exclusive; a single program can feature
multiple paradigms!

Basic concepts of oops

C++ is a multi-paradigm programming language. Meaning, it supports different programming


styles.

One of the popular ways to solve a programming problem is by creating objects, known as
object-oriented style of programming.

C++ supports object-oriented (OO) style of programming which allows you to divide complex
problems into smaller sets by creating objects.

Object is simply a collection of data and functions that act on those data.

C++ Class
Before you create an object in C++, you need to define a class.

A class is a blueprint for the object.

We can think of class as a sketch (prototype) of a house. It contains all the details about the
floors, doors, windows etc. Based on these descriptions we build the house. House is the
object.

As, many houses can be made from the same description, we can create many objects from a
class.

How to define a class in C++?


A class is defined in C++ using keyword class followed by the name of class.

The body of class is defined inside the curly brackets and terminated by a semicolon at the end.

class className

// some data

// some functions

};

Example: Class in C++


class Test
{
private:
int data1;
float data2;
public:
void function1()
{ data1 = 2; }

float function2()
{
data2 = 3.5;
return data2;
}
};

Here, we defined a class named Test.

This class has two data members: data1 and data2 and two member
functions: function1() and function2().

C++ Objects
When class is defined, only the specification for the object is defined; no memory or storage is
allocated.

To use the data and access functions defined in the class, you need to create objects.

Syntax to Define Object in C++


className objectVariableName;

You can create objects of Test class (defined in above example) as follows:

class Test
{
private:
int data1;
float data2;

public:
void function1()
{ data1 = 2; }

float function2()
{
data2 = 3.5;
return data2;
}
};

int main()
{
Test o1, o2;
}

Here, two objects o1 and o2 of Test class are created.

In the above class Test, data1 and data2 are data members and function1()and function2() are
member functions.

Object-Oriented Languages

Object based features+Inheritance+Dynamic Binding(Polymorphism)

__________________________________________________

*Object oriented languages support; the entire object based programming features along with inheritance and

dynamic binding.

*We can create new object.

Example: Small Talk,Simula ,C++ ,Java and Object Pascal.

Application of OOPs:

*Real-time system.
*Simulation and modeling.

*Object oriented data base.

*Hypertext and Hyper media.

*Artificial Intelligence and Expert System.

*Neural Networks and Parallel Programming.

*Decision Support and Office Automation System.

*Computer Aided Design/Computer Aided Manufacturing.

Benefits of oops

Advantages and Disadvantages of C++ language


Advantages of C++
 Object oriented

 Portable language (writing a program irrespective of operating system as well as


Hardware)

 Low-level language like Assembly language on Machine language called portable.

 C++ use multi-paradigm programming. The Paradigm means the style of


programming .paradigm concerned about logics, structure, and procedure of the program. C+
+ is multi-paradigm means it follows three paradigm Generic, Imperative, Object Oriented.

 It is useful for the low-level programming language and very efficient for general
purpose.

 C++ provide performance and memory efficiency.

 It provides a high-level abstraction.

 In the language of the problem domain.

 C++ is compatible with C.

 C++ used reusability of code.


 C++ used inheritance, polymorphism.

Portability

 Portability allows developing program irrespective of hardware

 It allows moving the program development for one platform to another platform

 C compatible (COMP): Programs developed in ‘C’ language can be moved without any
modifications into C++

 C++ is an object oriented embedded language which is having the characteristics of low-
level language & which is also developing the embedded software.

 Which language having low-level features

Disadvantages

 It has no security

 Complex in a very large high-level program.

 Used for platform specific application commonly.

 For a particular operating system or platform, the library set has usually chosen that
locks.

 When C++ used for web applications complex and difficult to debug.

 C++ can’t support garbage collection.

 C++ is not secure because it has a pointer, friend function, and global variable.

 No support for threads built in.

What is c++

C++ is a general-purpose, middle-level programming language, with high and low-level


programming capabilities, and this is one of the most popular commercial programming
languages.

A quick look at the example of C++ Program and detailed description is given in the C++
Program Structure page.

/*

* File: main.cpp
* Author: Gautam

* Created on April 28, 2018

*/

#include <iostream>

int main()

std::cout<<"This is my first C++ Program.";

std::cout<<std::endl<<"and its very easy to learn";

Program Output:

The above example has been used to print text on the screen.

Applications of C++

1. Games:
C++ overrides the complexities of 3D games, optimizes resource
management and facilitates multiplayer with networking. The language is
extremely fast, allows procedural programming for CPU intensive functions
and provides greater control over hardware, because of which it has been
widely used in development of gaming engines. For instance, the science
fiction game Doom 3 is cited as an example of a game that used C++ well
and the Unreal Engine, a suite of game development tools, is written in C+
+.

2. Graphic User Interface (GUI) based applications:


Many highly used applications, such as Image Ready, Adobe Premier,
Photoshop and Illustrator, are scripted in C++.

3. Web Browsers:
With the introduction of specialized languages such as PHP and Java, the
adoption of C++ is limited for scripting of websites and web applications.
However, where speed and reliability are required, C++ is still preferred. For
instance, a part of Google’s back-end is coded in C++, and the rendering
engine of a few open source projects, such as web browser Mozilla Firefox
and email client Mozilla Thunderbird, are also scripted in the programming
language.

4. Advance Computations and Graphics:


C++ provides the means for building applications requiring real-time
physical simulations, high-performance image processing, and mobile sensor
applications. Maya 3D software, used for integrated 3D modeling, visual
effects and animation, is coded in C++.

5. Database Software:
C++ and C have been used for scripting MySQL, one of the most popular
database management software. The software forms the backbone of a
variety of database-based enterprises, such as Google, Wikipedia, Yahoo and
YouTube etc.
6. Operating Systems:
C++ forms an integral part of many of the prevalent operating systems
including Apple’s OS X and various versions of Microsoft Windows, and the
erstwhile Symbian mobile OS.

7. Enterprise Software:
C++ finds a purpose in banking and trading enterprise applications, such as
those deployed by Bloomberg and Reuters. It is also used in development of
advanced software, such as flight simulators and radar processing.

8. Medical and Engineering Applications:


Many advanced medical equipments, such as MRI machines, use C++
language for scripting their software. It is also part of engineering
applications, such as high-end CAD/CAM systems.

9. Compilers:
A host of compilers including Apple C++, Bloodshed Dev-C++, Clang C++
and MINGW make use of C++ language. C and its successor C++ are
leveraged for diverse software and platform development requirements,
from operating systems to graphic designing applications. Further, these
languages have assisted in the development of new languages for special
purposes like C#, Java, PHP, Verilog etc.

A simple C++ Program

A simple C++ program to display "Hello, World!" on the screen. Since, it's a very simple
program, it is often used to illustrate the syntax of a programming language.
Example 1: Hello World Program
#include <iostream>
using namespace std;

int main()
{
cout << "Hello, World!";
return 0;
}

Output

Hello, World!

Every C++ program starts from the main() function.

The cout is the standard output stream which prints the "Hello, World!" string on the monitor.

The return 0; is the Exit status" of the program.

More C++ statements

Statements

Statements

Statements are fragments of the C++ program that are executed in sequence. The body of any function is a
sequence of statements. For example:

int main()
{
int n = 1; // declaration statement
n = n + 1; // expression statement
std::cout << "n = " << n << '\n'; // expression statement
return 0; // return statement
}
C++ includes the following types of statements:

1) expression statements;

2) compound statements;

3) selection statements;

4) iteration statements;

5) jump statements;

6) declaration statements;

7) try blocks;

8) atomic and synchronized blocks (TM TS).

Labels
Any statement can be labeled, by providing a label followed by a colon before the statement itself.

attr(optional) identifier : statement (1)

attr(optional) case constexpr : statement (2)

attr(optional) default : statement (3)

1) target for goto;

2) case label in a switch statement;

3) default label in a switch statement.

An attribute sequence attr may appear just before the label (in which case it applies to the label), or just before
any statement itself, in which case it applies to the entire statement. A statement may carry multiple labels.
Labels (and only labels) have function scope. Labels are ignored by unqualified lookup: a label can have the
same name as any other entity in the program.

Expression statements
An expression followed by a semicolon is a statement.

attr(optional) expression(optional) ; (1)

attr(C++11) - optional sequence of any number of attributes

expression - an expression

Most statements in a typical C++ program are expression statements, such as assignments or function calls.
An expression statement without an expression is called a null statement. It is often used to provide an empty
body to a for or while loop. It can also be used to carry a label in the end of a compound statement.

Compound statements
Compound statements or blocks are brace-enclosed sequences of statements.

attr(optional) { statement...(optional) } (1)

When one statement is expected, but multiple statements need to be executed in sequence (for example, in
an ifstatement or a loop), a compound statement may be used:

if (x > 5) // start of if statement


{ // start of block
int n = 1; // declaration statement
std::cout << n; // expression statement
} // end of block, end of if statement

Each compound statement introduces its own block scope; variables declared inside a block are destroyed at
the closing brace in reverse order:

int main()
{
{ // start of block
std::ofstream f("test.txt"); // declaration statement
f << "abc\n"; // expression statement
} // end of block: f is flushed and closed
std::ifstream f("test.txt");
std::string str;
f >> str;
}
Selection statements
Selection statements choose between one of several flows of control.

attr(optional) if ( condition ) statement (1)

attr(optional) if ( condition ) statement else statement (2) (until C++17)

attr(optional) switch ( condition ) statement (3)

attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement (1)


(since C+
+17)
attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement
(2)
elsestatement
attr(optional) switch ( init-statement(optional) condition ) statement (3)

1) if statement;

2) if statement with an else clause;

3) switch statement.

Iteration statements
Iteration statements repeatedly execute some code.

attr(optional) while ( condition ) statement (1)

attr(optional) do statement while ( expression ) ; (2)

attr(optional) for ( init-statement condition(optional) ; expression(optional) ) statement (3)

attr(optional) for ( for-range-decl : for-range-init ) statement (4) (since C++11)

1) while loop;

2) do-while loop;

3) for loop;

4) range for loop.

Jump statements
Jump statements unconditionally transfer flow control

attr(optional) break ; (1)

attr(optional) continue ; (2)

attr(optional) return expression(optional) ; (3)

attr(optional) return braced-init-list ; (4) (since C++11)

attr(optional) goto identifier ; (5)

1) break statement;

2) continue statement;

3) return statement with an optional expression;


4) return statement using list initialization;

5) goto statement.

Note: for all jump statements, transfer out of a loop, out of a block, or back past an initialized variable with
automatic storage duration involves the destruction of objects with automatic storage duration that are in scope
at the point transferred from but not at the point transferred to. If multiple objects were initialized, the order of
destruction is the opposite of the order of initialization.

Declaration statements
Declaration statements introduce one or more identifiers into a block.

block-declaration ; (1)

1) see Declarations and Initialization for details.

Try blocks
Try blocks provide the ability to catch exceptions thrown when executing other statements.

attr(optional) try compound-statement handler-sequence (1)

1) see try/catch for details.

Atomic and synchronized blocks


Atomic and synchronized blocks are used to implement transactional memory.

synchronized compound-statement (1) (TM TS)

atomic_noexcept compound-statement (2) (TM TS)

atomic_cancel compound-statement (3) (TM TS)

atomic_commit compound-statement (4) (TM TS)

synchronized block, executed in single total order with all synchronized blocks;

atomic block that aborts on exceptions;

atomic block that rolls back on exceptions;

atomic block that commits on exceptions.

Reference Variables
Variable Definition in C++
A variable definition means that the programmer writes some instructions to tell the compiler to
create the storage in a memory location.

The syntax for defining variables is:


Syntax:

data_type variable_name;

data_type variable_name, variable_name, variable_name;

Here data_type means the valid C++ data type which includes int, float, double, char, wchar_t,
bool and variable list is the lists of variable names to be declared which is separated by
commas.
Example:

/* variable definition */int width, height, age;

char letter;

float area;

double d;

Variable Initialization in C++


Variables are declared in the above example but none of them has been assigned any value.
Variables can be initialized and the initial value can be assigned along with their declaration.
Syntax:

data_type variable_name = value;

Example:

/* variable definition and initialization */int width, height=5, age=32;

char letter='A';

float area;
double d;

/* actual initialization */width = 10;

area = 26.5;

There is some rules must be in your knowledge to work with C++ variables.

Rules of Declaring variables in C++


 A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and
the underscore character.

 The first character must be a letter or underscore.

 Blank spaces cannot be used in variable names.

 Special characters like #, $ are not allowed.

 C++ keywords cannot be used as variable names.

 Variable names are case-sensitive.

 A variable name can be consisting of 31 characters only if we declare a variable more


than 1 characters compiler will ignore after 31 characters.

 Variable type can be bool, char, int, float, double, void or wchar_t.

Operators in C++ -

C++ operator is a symbol that is used to perform mathematical or logical manipulations. C++
language is rich with built-in operators.

Arithmetic Operators
Operato Description
r

+ Addition
- Subtraction

* Multiplication

/ Division

% Modulus

Increment and Decrement Operators


Operato Description
r

++ Increment

−− Decrement

Relational Operators
Operato Description
r

== Is equal to

!= Is not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to


Logical Operators
Operato Description
r

&& And operator. Performs a logical conjunction of two


expressions.
(if both expressions evaluate to True, result is True. If either
expression evaluates to False, result is False)

|| Or operator. Performs a logical disjunction on two


expressions.
(if either or both expressions evaluate to True, result is
True)

! Not operator. Performs logical negation on an expression.

Bitwise Operators
Operato Description
r

<< Binary Left Shift Operator

>> Binary Right Shift Operator

~ Binary One's Complement Operator

& Binary AND Operator

^ Binary XOR Operator

| Binary OR Operator
Assignment Operators
Operato Description
r

= Assign

+= Increments, then assigns

-= Decrements, then assigns

*= Multiplies, then assigns

/= Divides, then assigns

%= Modulus, then assigns

<<= Left shift and assigns

>>= Right shift and assigns

&= Bitwise AND assigns

^= Bitwise exclusive OR and assigns

|= Bitwise inclusive OR and assigns

Misc Operators
Operato Description
r

, Comma operator

sizeof() Returns the size of an memory location.

& Returns the address of an memory location.

* Pointer to a variable.

?: Conditional Expression

S-ar putea să vă placă și