[go: up one dir, main page]

0% found this document useful (0 votes)
15 views35 pages

Unit 1

Uploaded by

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

Unit 1

Uploaded by

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

UNIT 1

• C++ was developed by Bjarne Stroustrup, as an extension


to the C language.
• C++ is an object-oriented programming language which
gives a clear structure to programs and allows code to be
reused.
• C++ is portable and can be used to develop applications
that can be adapted to multiple platforms.
Example program
#include <iostream>
using namespace std;

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

• #include <iostream>

int main() {
std::cout << "Hello World!";
return 0;
}
Addition program
• #include <iostream>
• using namespace std;
• int main() {
• int a=8,b=7,c;
• c=a+b;
• cout << "sum="<<c;
• return 0;
• }
Variables

• Variables are containers for storing data values.


• different types of variables:
• int - stores integers (whole numbers), without decimals, such as 123 or -
123
• double - stores floating point numbers, with decimals, such as 19.99 or -
19.99
• char - stores single characters, such as 'a' or 'B'.
• string - stores text, such as "Hello World".
• bool - stores values with two states: true or false
Constants

• When you do not want others (or yourself) to change


existing variable values, use the const keyword (this will
declare the variable as "constant", which means
unchangeable.
• eg:
• const int myNum = 15; // myNum will always be 15
• myNum = 10; // error
Data Types
Data Size Description
Type
boolean 1 byte Stores true or false values
char 1 byte Stores a single character/letter/number, or
ASCII values
int 2 or 4 Stores whole numbers, without decimals
bytes
float 4 bytes Stores fractional numbers, containing one or
more decimals. Sufficient for storing 6-7
decimal digits
double 8 bytes
Operators

• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
Control Structures
• if Statement
• if (condition) {
// block of code to be executed if the condition is true
}
• if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
else if Statement

• if (condition1) {
// block of code to be executed if condition1 is
true
} else if (condition2) {
// block of code to be executed if the condition1 is
false and condition2 is true
} else {
// block of code to be executed if the condition1 is
false and condition2 is false
}
Greatest of two numbers
• #include <iostream>
• using namespace std;
• int main() {
• int num1, num2;
• cout << "Enter two numbers: ";
• cin >> num1 >> num2;
• if (num1 > num2) {
• cout<<num1<<" is greater";
• }
• else
• {
• cout<<num2<<"is greater";
• }

• return 0;
• }
Greatest of three numbers
• #include <iostream>
• using namespace std;
• int main() {
• int num1, num2, num3;
• cout << "Enter three numbers: ";
• cin >> num1 >> num2 >> num3;
• if ((num1 > num2) && (num1>num3))
• {
• cout<<num1<<" is greater";
• }
• else if ((num2> num1) && (num2>num3))
• {
• cout<<num2<<"is greater";
• }
• else
• cout<<num3<<"is greater";
• return 0;
• }
loops

• Three types of Loops in C++:


• While Loop
• Do While Loop
• For Loop
Addition program with user input
• #include <iostream>
• using namespace std;
• int main() {
• int a,b,c;
• cout<<"enter two numbers:";
• cin>>a>>b;
• c=a+b;
• cout << "sum="<<c;
• return 0;
• }
Object-oriented programming
• Object-oriented programming (OOP) is a computer
programming model that organizes software design around
data, or objects, rather than functions and logic.
basic concepts of C++
• Class
• Object
• Encapsulation
• Abstraction
• Polymorphism
• Inheritance
• Dynamic Binding
• Message Passing
Class

• user-defined data type, which holds its own data members


and member functions
• A class is like a blueprint for an object.
• In class Car, the data member will be speed limit, mileage,
etc and member functions can apply brakes, increase
speed, etc.
Object

• An Object is an identifiable entity with some


characteristics and behavior.
• An Object is an instance of a Class.
• When a class is defined, no memory is allocated but when
it is instantiated (i.e. an object is created) memory is
allocated.
Ex pgm
• #include <iostream>
• using namespace std;
• class person {
• char name[20];
• int id;
• public:
• void getdetails() {}
• };
• int main()
• {
• person p1; // p1 is a object
• return 0;
• }
Encapsulation

• Encapsulation is defined as binding together the data and the functions that
manipulate them.
• Consider a real-life example of encapsulation, in a company, there are different
sections like the accounts section, finance section, sales section, etc. The
finance section handles all the financial transactions and keeps records of all
the data related to finance. Similarly, the sales section handles all the sales-
related activities and keeps records of all the sales. Now there may arise a
situation when for some reason an official from the finance section needs all the
data about sales in a particular month. In this case, he is not allowed to directly
access the data of the sales section. He will first have to contact some other
officer in the sales section and then request him to give the particular data. This
is what encapsulation is.
Abstraction

• Data abstraction refers to providing only essential


information about the data to the outside world, hiding the
background details or implementation.
• Consider a real-life example of a man driving a car. The
man only knows that pressing the accelerator will
increase the speed of the car or applying brakes will stop
the car but he does not know how on pressing the
accelerator the speed is actually increasing, he does not
know about the inner mechanism of the car or the
implementation of an accelerator, brakes, etc. in the car.
This is what abstraction is.
Polymorphism

• polymorphism means having many forms.


• A person at the same time can have different
characteristics. A man at the same time is a father, a
husband, and an employee. So the same person possesses
different behavior in different situations. This is called
polymorphism.
• Operator Overloading: The process of making an operator
exhibit different behaviors in different instances is known
as operator overloading.
• Function Overloading: Function overloading is using a
single function name to perform different types of tasks.
Inheritance

• The capability of a class to derive properties and


characteristics from another class is called Inheritance.
• Sub Class: The class that inherits properties from another
class is called Sub class or Derived Class.
• Super Class: The class whose properties are inherited by a
sub-class is called Base Class or Superclass.
• Reusability: Inheritance supports the concept of
“reusability”, i.e. when we want to create a new class and
there is already a class that includes some of the code that
we want, we can derive our new class from the existing
class.
Dynamic Binding

• In dynamic binding, the code to be executed in response


to the function call is decided at runtime.
• C++ has virtual functions to support this.
Message Passing

• Objects communicate with one another by sending and


receiving information.
• A message for an object is a request for the execution of a
procedure and therefore will invoke a function in the
receiving object that generates the desired results.
Object-Oriented Language

• Object-oriented language typically supports the following


features, at minimum:
• The ability to create classes and their associated objects
• Encapsulation
• Inheritance
• Java, C++ and Smalltalk are popular examples of object-
oriented languages.
Applications of Object-Oriented
Programming
• Client-Server Systems. ...
• Object Oriented Databases. ...
• Real-Time System Design. ...
• Simulation and Modeling Systems. ...
• Hypertext and Hypermedia. ...
• Neural Networking and Parallel Programming. ...
• Office Automation Systems. ...
• CIM/CAD/CAM Systems.
Tokens
• A token is a sequence of characters that represents a
specific element in the program, such as a keyword,
variable, operator, or punctuation symbol.
• C language has six types of tokens: keywords, identifiers,
constants, operators, special symbols, and strings.
Keywords
• reserved words that have a specific meaning and purpose
in a programming language
Type compatibility
• The type compatibility is being able to use two types
together without modification and being able to substitute
one for the other without modification.
• For example
• float n1=12.5;
• int n2=n1;
Implicit Conversions
• It is a Type Conversion in C++
• Implicit Type Conversion also known as ‘automatic type
conversion’.
• Done by the compiler on its own, without any external
trigger from the user.
Example of Type Implicit Conversion
• #include <iostream>
• using namespace std;
• int main()
• {
• int x = 10; // integer x
• // x is implicitly converted to float
• float z = x + 1.0;
• cout << "x = " << x << endl << "z = " << z << endl;
• return 0;
• }
Operator Precedence
• Operator precedence specifies the order of operations in
expressions that contain more than one operator.

You might also like