Date post: | 08-Jul-2015 |
Category: |
Education |
Upload: | sikder-tahsin-al-amin |
View: | 125 times |
Download: | 0 times |
INTRODUCTION TO C++
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.
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.
Class
• A class is a collection of objects of similar type.
• Mango, Apple etc. are objects and their class is Fruit.
Message Passing
• Information or message of one object can be sent to other
object.
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.
Inheritance
• Inheritance is the process of one class acquire the
properties of another class.
Polymorphism
• One name, multiple forms.
• Allows us to have more than one function with the same
name in a program.
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.
Structure of a program
int main()
{
-------------
return 0;
}
A simple C++ program
• // my first program in C++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
Hello World
#include <iostream>
using namespace std;
int main()
{
cout << "Hello” <<endl << “World!";
return 0;
}
Hello
World
Comment
• C++ supports two ways of commenting code:
• // line comment
• /* block comment */
Initialization of variables
• int a=5; // initial value: 5
• int b(3); // initial value: 3
• int c{2}; // initial value: 2
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.
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.
Reference Variable
• Provides an alias (alternate name) for a previously
defined variables.
• data _type & reference_name = variable_name
• float total = 100;
• float & sum = total;
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
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 (\)
Arithmetic Operators
operator description
+ addition
- subtraction
* multiplication
/ division
% modulo
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.
Thank you