Introduction to C++
Introduction
• C++ is a powerful, high-performance, general-
purpose programming language that supports
procedural, object-oriented, and generic
programming paradigms.
• Developed by Bjarne Stroustrup in 1979 as an
extension of the C language, it offers a high
degree of control over system resources and
memory.
Features of C++
• Simple
• Machine Independent
• Low-level Access
• Fast Execution Speed
• Object-Oriented
Uses of C++
• Operating Systems & GUIs
• Game Development
• Embedded Systems
• Financial and Scientific Applications
Structure of C++ Program
• Header File: #include <iostream> adds input/output
objects (cin, cout, etc.) via the preprocessor.
• Namespace Declaration: using namespace std; allows
direct use of standard names like "cout" without std::
• Main Function: int main() is the program’s entry point;
execution starts here
• Comments: // for single line, /*....*/ for multi-line are
ignored by the compiler and used only for code
documentation.
• Statement: Contains executable code.
• Return: The return 0; statement terminates the main()
function and indicates that the program executed
successfully.
cout
• used to display data (text, numbers, etc.) on
the console or screen. It stands for "character
output".
• uses the insertion operator (<<), often
pronounced "put to", to insert data into the
output stream.
• The << operator can be chained to output
multiple values in a single statement
cin
• used to read data provided by the user from
the standard input device, typically the
keyboard. It stands for "character input".
• It uses the extraction operator (>>), often
pronounced "get from", to extract data from
the input stream and store it in a variable.
Example Program: Addition of two
numbers
#include<iostream.h>
using namespace std;
int main( ) {
int a,b,sum;
cout<<“Enter two numbers \n”;
cin>>a>>b;
sum=a+b;
cout<<“Sum is”<<sum;
return 0;
}
Programs to do
1. C++ program to swap three numbers
2. C++ program to find biggest of three numbers
3. C++ program for multiplication table
4. C++ program to Convert Fahrenheit to Celsius
5. C++ program to Calculate simple interest
6. C++ program to Calculate area and perimeter of
rectangle
7. C++ Program to Find Quotient and Remainder
8. C++ Program to Check Whether Number is Even
or Odd
1. C++ Program to Calculate Sum of Natural
Numbers
2. C++ Program to Find Factorial of a number
3. C++ Program to Display Fibonacci Series
4. C++ Program to Reverse a Number
5. C++ Program to Calculate Power of a Number
6. C++ Program to Find ASCII Value of a Character
7. C++ program to Compute the power a given
number to a given power.
8. C++ program to find reverse of a number.
Basic Concepts
• Class
• Objects
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Class
• A class is a blueprint or a template for creating
objects.
• It is a user-defined data type that defines a set
of properties (data members) and behaviors
(member functions) that all objects of that
class will share.
• When a class is defined, no memory is
allocated.
Objects
• An object is an instance of a class.
• It is a real-world entity that has an identity,
attributes, and behaviors.
• When an object is created from a class,
memory is allocated, and the object can
interact with other objects through message
passing (function calls).
Encapsulation
• This is the mechanism of wrapping the data
(attributes) and the code (methods) into a
single unit, which is the class.
• The data is hidden from the outside world
• It can only be accessed through the methods
defined within the class, providing data
security and control
Abstraction
• Abstraction is the process of hiding the
complex implementation details and showing
only the essential features of an object to the
user.
• This simplifies the user interface and improves
code readability and maintenance.
Inheritance
• This is the ability of a class to derive
properties and characteristics from another
class.
• The class that inherits is called the derived (or
child) class, and the class from which
properties are inherited is called the base (or
parent) class.
Polymorphism
• Meaning "many forms", polymorphism allows
a single function or operator to behave
differently depending on the context or the
type of data it operates on.
• In C++, it can be achieved through function
overloading (same function name, different
parameters), operator overloading, and
function overriding using virtual functions.
Dynamic Binding
• Also known as late binding, this means that
the code associated with a given function call
is not known until the time of the call at
runtime.
• This is primarily achieved in C++ using virtual
functions
Message Passing
• Objects communicate with each other by
sending and receiving information, typically by
invoking methods (functions) on other
objects.
Classes and Objects
Class
• A class is a blueprint or a template for creating
objects.
• Syntax:
class ClassName {
access_specifier:
data members;
member functions;
};
• Data Members
– Variables declared inside a class
– Represent properties of an object
• Member Functions
– Functions declared inside a class
– Define the behavior of the object
• Access Specifiers
– Control accessibility of members
– private, protected, public
Objects
• An object is an instance of a class
• Syntax:
class_name object_name;
• Eg.
student s;
#include <iostream.h> void display() {
using namespace std; cout << "Roll: " << roll <<
endl;
class Student {
cout << "Marks: " << marks
int roll; << endl;
float marks; }
public: };
void getData() { int main() {
cout << "Enter roll and Student s;
marks: "; [Link]();
cin >> roll >> marks; [Link]();
} return 0;
}
Access Specifiers
• define the visibility and accessibility of data
and functions
• help in data hiding, security, and
encapsulation
• Types of Access Specifiers in C++
– private
– protected
– public
#include <iostream> void show() {
using namespace std; cout << a << endl;
class Example { cout << b << endl;
private: cout << c << endl;
int a = 10; }
protected: };
int b = 20; int main() {
public: Example obj;
int c = 30; [Link]();
obj.a = 5; // Error
obj.b = 10; // Error
obj.c = 15; // Allowed
}
Programs to do
1. Class to Find Area of Rectangle
2. Class to Find Sum of Two Numbers
3. Student Details Using Class
4. Class for Bank Account
5. Class to check palindrome number
Programs to do
1. Bank account
1. Data Members
1. accNo, balance → private
2. Name -> public
2. Member Functions
1. Balancecheck(), deposit(), withdraw() → public
Deposit – ( Minimum amount for deposit will be 100)
Withdraw – ( After withdraw if balance is <1000, then stop
the operation)
Balance Check