A Look at Procedure C++ (Object Oriented Program)
A Look at Procedure C++ (Object Oriented Program)
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 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.
*Object-oriented programming used to solve mathematical problems and real world problems.
*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
*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
C++ OOP's
*The fundamental idea behind OOP is to combine both data and functions that operate on that data into a single
that objects.
Features of OOP:-
Declarative: Programming by specifying the result you want, not how to get it.
Functional (Applicative): Programming with function calls that avoid any global state.
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!
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.
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.
The body of class is defined inside the curly brackets and terminated by a semicolon at the end.
class className
// some data
// some functions
};
float function2()
{
data2 = 3.5;
return data2;
}
};
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.
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;
}
In the above class Test, data1 and data2 are data members and function1()and function2() are
member functions.
Object-Oriented Languages
__________________________________________________
*Object oriented languages support; the entire object based programming features along with inheritance and
dynamic binding.
Application of OOPs:
*Real-time system.
*Simulation and modeling.
Benefits of oops
It is useful for the low-level programming language and very efficient for general
purpose.
Portability
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.
Disadvantages
It has no security
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++ is not secure because it has a pointer, friend function, and global variable.
What is c++
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
*/
#include <iostream>
int main()
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+
+.
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.
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.
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 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!
The cout is the standard output stream which prints the "Hello, World!" string on the monitor.
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;
Labels
Any statement can be labeled, by providing a label followed by a colon before the statement itself.
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.
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.
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:
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.
1) if statement;
3) switch statement.
Iteration statements
Iteration statements repeatedly execute some code.
1) while loop;
2) do-while loop;
3) for loop;
Jump statements
Jump statements unconditionally transfer flow control
1) break statement;
2) continue statement;
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)
Try blocks
Try blocks provide the ability to catch exceptions thrown when executing other statements.
synchronized block, executed in single total order with all synchronized blocks;
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.
data_type 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:
char letter;
float area;
double d;
Example:
char letter='A';
float area;
double d;
area = 26.5;
There is some rules must be in your knowledge to work with C++ variables.
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
−− Decrement
Relational Operators
Operato Description
r
== Is equal to
!= Is not equal to
Bitwise Operators
Operato Description
r
| Binary OR Operator
Assignment Operators
Operato Description
r
= Assign
Misc Operators
Operato Description
r
, Comma operator
* Pointer to a variable.
?: Conditional Expression