+ All Categories
Home > Education > Introduction to C++

Introduction to C++

Date post: 08-Jul-2015
Category:
Upload: sikder-tahsin-al-amin
View: 125 times
Download: 0 times
Share this document with a friend
Description:
An introduction to object oriented programming concepts and c++ program structure
Popular Tags:
22
INTRODUCTION TO C++
Transcript
Page 1: Introduction to C++

INTRODUCTION TO C++

Page 2: 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.

Page 3: Introduction to C++

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.

Page 4: Introduction to C++

Class

• A class is a collection of objects of similar type.

• Mango, Apple etc. are objects and their class is Fruit.

Page 5: Introduction to C++

Message Passing

• Information or message of one object can be sent to other

object.

Page 6: Introduction to C++

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.

Page 7: Introduction to C++

Inheritance

• Inheritance is the process of one class acquire the

properties of another class.

Page 8: Introduction to C++

Polymorphism

• One name, multiple forms.

• Allows us to have more than one function with the same

name in a program.

Page 9: Introduction to C++

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.

Page 10: Introduction to C++

Structure of a program

int main()

{

-------------

return 0;

}

Page 11: Introduction to C++

A simple C++ program

• // my first program in C++

#include <iostream>

using namespace std;

int main()

{

cout << "Hello World!";

return 0;

}

Hello World

Page 12: Introduction to C++

#include <iostream>

using namespace std;

int main()

{

cout << "Hello” <<endl << “World!";

return 0;

}

Hello

World

Page 13: Introduction to C++

Comment

• C++ supports two ways of commenting code:

• // line comment

• /* block comment */

Page 14: Introduction to C++

Initialization of variables

• int a=5; // initial value: 5

• int b(3); // initial value: 3

• int c{2}; // initial value: 2

Page 15: Introduction to C++

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.

Page 16: Introduction to C++

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.

Page 17: Introduction to C++

Reference Variable

• Provides an alias (alternate name) for a previously

defined variables.

• data _type & reference_name = variable_name

• float total = 100;

• float & sum = total;

Page 18: Introduction to C++

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

Page 19: Introduction to C++

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 (\)

Page 20: Introduction to C++

Arithmetic Operators

operator description

+ addition

- subtraction

* multiplication

/ division

% modulo

Page 21: Introduction to C++

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.

Page 22: Introduction to C++

Thank you


Recommended