0% found this document useful (0 votes)
20 views9 pages

Concept of C++

Uploaded by

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

Concept of C++

Uploaded by

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

Concept of C++

• Introduction of C++
C++ is an extended version of the C programming
language, developed by Bjarne Stroustrup. It builds upon
the procedural foundations of C and introduces
powerful features like object-oriented programming and
templates. Due to its speed, flexibility, and efficiency,
C++ is widely used in system programming, game
development, embedded systems, and real-time
applications.
Object Oriented Programming:
“Object oriented programming as an approach that
provides a way of modularizing programs by creating
partitioned memory area for both data and functions
that can be used as templates for creating copies of
such modules on demand”.
Features of the Object Oriented programming:
1. Emphasis is on doing rather than procedure.
2. programs are divided into what are known as objects.
3. Data structures are designed such that they
characterize the objects.
4. Functions that operate on the data of an object are
tied together in the data structure.
5. Data is hidden and can’t be accessed by external
functions.
6. Objects may communicate with each other through
functions.
7. New data and functions can be easily added.
8. Follows bottom-up approach in program design.
Class:
A class is a user-defined blueprint or template for
creating objects. It defines properties (data members)
and behaviours (member functions) of an object.
Classes help organize code in a modular and reusable
way.

Object:
An object is an instance of a class. It represents a real-
world entity with attributes and behaviours. Once a class
is defined, we can create multiple objects from it, each
with its own copy of data.
Encapsulation:
Encapsulation means wrapping the data (variables) and
the code (functions) that operate on the data into a
single unit, i.e., a class. It protects internal data from
unauthorized access and provides a clear structure to
the code.
Abstraction:
Abstraction is the concept of hiding the internal
implementation details and showing only the necessary
information. In C++, it allows users to interact with
objects without needing to know the inner workings.
Inheritance
Inheritance allows a class (called the derived or child
class) to inherit properties and methods from another
class (called the base or parent class). This promotes
code reuse and helps in organizing related classes.
Polymorphism:
Polymorphism means "many forms." In C++, it allows
the same function name or operator to behave
differently in different contexts. It can be achieved
through function overloading (compile-time) and
function overriding using virtual functions (runtime).

Graduating to C++
1. Setting Up and Basic Concepts

• To run C++ programs, a compiler is required. This book


uses Visual Studio Express but is compatible with other
compilers.
• Visual Studio is a helpful IDE that integrates tools for
program development.
• Assumes the reader has a working knowledge of C.
2. C++ Background

• Developed in the early 1980s by Bjarne Stroustrup at Bell


Labs.
• Designed as an extension of C with object-oriented
programming (OOP) capabilities.
• C++ adds features to C without removing any, making it a
superset of C.
• Valid C programs are generally valid C++ programs too.
• New keywords introduced in C++ include:
• new, class, namespace, try, catch, throw, template,
mutable, public, private, protected, etc.

3. Input/Output Using cout and cin

• cout is used for output (standard output stream), and <<


is the insertion operator.
• Example: cout << "God, Give me common
sense!";
• cin is used for input (standard input stream), and >> is
the extraction operator.
• Example program:

char str[40];
int m1, m2, m3, avg;
cin >> str >> m1 >> m2 >> m3;
avg = (m1 + m2 + m3)/3;
cout << "Name: " << str << ", Avg: " << avg;
4. Namespace and std

• All standard C++ library identifiers belong


to the namespace std.
• Two ways to use it:
o using namespace std;
o std::cout, std::cin, etc.

5. endl and newline

• endl inserts a newline and flushes the


output stream.
• Alternate newline options:

cout << "\nMessage";


cout << '\n' << "Message";

6. Advantage Over scanf/printf

• In cin and cout, you don’t need format


specifiers like %d or %s, making code
cleaner.
• Type safety is better and output is more
C++-styled.

7. Function Prototypes

• Function prototypes declare return type and


parameters:

float square(float);
char* strconvert(char*, int);
double nthroot(float, float);
• Enforces strong type checking before a
function is called.
• Example:

char str[] = "Hello";


strupr(str); // Requires including <string.h>

8. Comments in C++

• Supports both:
o // single-line comment
o /* */ multi-line comment

9. Flexible Declarations

• Unlike C, C++ allows variables to be


declared at the point of use, not
necessarily at the beginning of a function.

int f;
cin >> f;
int c = (f - 32) * 5/9;

10. Structures, Unions, Enums

• Structures:

struct employee { char name[20]; int age; };

• Unions:

union data { char ch[2]; int i; };

• Enums:
enum status { married, unmarried, divorced };

• C++ doesn’t require writing struct, union,


or enum when defining variables.

11. Anonymous Unions and Enums

• Anonymous unions allow direct access without


a variable name:

union { int i; char ch[2]; };


i = 10; ch[0] = 'A';

12. Typecasting

• C++ supports two typecasting styles:

n = (y - 1) * j; // May cause
overflow
n = (y - 1) * (long) j; // C-style
casting.

You might also like