This document provides an introduction to object-oriented programming concepts in C++ including objects, classes, encapsulation, inheritance, polymorphism, and more. It explains the basic structure of a C++ program and demonstrates how to write a simple "Hello World" program. It also covers basic C++ elements like variables, data types, comments, strings, arithmetic operators, and input/output.
2. What is OOP
• Object-oriented programming (OOP) is a
programming paradigm that represents the
concept of "objects" that have data fields
(attributes that describe the object) and
associated procedures known as methods.
3. Objects
• Basic run time entities in OOP.
• In real world may be a cars, person, bank account etc.
• Have attributes and behaviors.
• Cars has colors and speed.
• In programming, attributes are variables and behaviors
are functions.
4. Class
• A class is a collection of objects of similar type.
• Mango, Apple etc. are objects and their class is Fruit.
5. Message Passing
• Information or message of one object can be sent to other
object.
6. Encapsulation
• Wrapping up of data and functions into a single unit
(class) is called encapsulation
• Data is not accessible to outside world.
• Only those functions which are which are wrapped in the
class can access it.
8. Polymorphism
• One name, multiple forms.
• Allows us to have more than one function with the same
name in a program.
9. Why OOP over C
• Code Reusability (one class code in other)
• Data is hidden and can’t be accessed by external
functions.
• New data and functions can be easily added.
• Maintenance
• Software complexity can be reduced.
• Can be easily upgraded from small to large systems.
10. Structure of a program
int main()
{
-------------
return 0;
}
11. A simple C++ program
• // my first program in C++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
Hello World
12. #include <iostream>
using namespace std;
int main()
{
cout << "Hello” <<endl << “World!";
return 0;
}
Hello
World
13. Comment
• C++ supports two ways of commenting code:
• // line comment
• /* block comment */
14. Initialization of variables
• int a=5; // initial value: 5
• int b(3); // initial value: 3
• int c{2}; // initial value: 2
15. Type deduction: auto and decltype
• int foo = 0;
• auto bar = foo; // the same as: int bar = foo;
• Here, bar is declared as having an auto type; therefore,
the type of bar is the type of the value used to initialize it:
in this case it uses the type of foo, which is int.
16. Type deduction: auto and decltype
• Variables that are not initialized can also make use of type
deduction with the decltype specifier:
• int foo = 0;
decltype(foo) bar; // the same as: int bar;
• Here, bar is declared as having the same type as foo.
17. Reference Variable
• Provides an alias (alternate name) for a previously
defined variables.
• data _type & reference_name = variable_name
• float total = 100;
• float & sum = total;
18. Introduction to strings
• // my first string
• #include <iostream>
• #include <string>
• using namespace std;
• int main ()
• {
• string mystring;
• mystring = "This is a string";
• cout << mystring;
• return 0;
• }
This is a string
19. Some coding arrangement
n newline
r carriage return
t tab
v vertical tab
b backspace
f form feed (page feed)
a alert (beep)
' single quote (')
" double quote (")
? question mark (?)
backslash ()
21. Basic Input/Output
• // i/o example of taking a number, display and double it
• #include <iostream>
• using namespace std;
• int main ()
• {
• int i;
• cout << "Please enter an integer value: ";
• cin >> i;
• cout << "The value you entered is " << i;
• cout << " and its double is " << i*2 << ".n";
• return 0;
• } Please enter an integer value: 702
The value you entered is 702 and its double is 1404.