Transcript
Page 1: Basic Concepts of OOP in C++

Basic Concepts of OOP in C++

Darvay Zsolt

Page 2: Basic Concepts of OOP in C++

C++ 2

Outline

The namespace and its members The using declaration and directive The address operator and the

reference type The scope operator The type identifier operator Comparison of simle Java and C++

codes

Page 3: Basic Concepts of OOP in C++

C++ 3

Outline

Data protection using modular programming in C

Abstract Data Types Classes

Page 4: Basic Concepts of OOP in C++

C++ 4

The Namespace and its Members

The namespace concept Example Accessing the members of the

namespace

Page 5: Basic Concepts of OOP in C++

C++ 5

The Namespace Concept

With a namespace one can attain the grouping of some declarations.

The names which are interconnected to each other will belong to the same namespace.

The definition of a namespace:namespace name {

// declarations, definitions}

Page 6: Basic Concepts of OOP in C++

C++ 6

Example

#include <iostream>namespace MyVector {

int *elem;int dim;void Init(int *e, int d);void FreeVect();void SquareVect();void Display();

}

A vector namespace with integer elements

Page 7: Basic Concepts of OOP in C++

C++ 7

Accessing the Members of the Namespace

We use the scope ( :: ) operator. Accessing: namespace_name::member. Example: MyVector::dim = 6; This is valid also for functions. We use

the form namespace_name::function. Simple access with the using declaration

and directive.

Page 8: Basic Concepts of OOP in C++

C++ 8

The using Declaration

To access more then one time a member: using declaration

The declaration:using namespace_name::member;

Example: using MyVector::dim; Then dim = 14;

is the same as MyVector::dim = 14;.

Page 9: Basic Concepts of OOP in C++

C++ 9

The using Directive

Simple access of all members Form:using namespace namespace_name;

Example: using namespace MyVector;

The global using directives are used for compatibility issues with older C++ versions.

Page 10: Basic Concepts of OOP in C++

C++ 10

The iostream Header File

//with using directive

#include <iostream>using namespace std;void Print() {...cout << endl;}

//without using directive

#include <iostream>void Print() {...std::cout <<

std::endl;}

Page 11: Basic Concepts of OOP in C++

C++ 11

The Init function

void MyVector::Init(int *e, int d){

elem = new int[d];dim = d;for(int i=0; i < dim; i++)

elem[i] = e[i];}

Page 12: Basic Concepts of OOP in C++

C++ 12

The FreeVect and SquareVect functions

void MyVector:: FreeVect() {delete []elem;

}void MyVector:: SquareVect() {

for(int i = 0; i < dim; i++)elem[i] *= elem[i];

}

Page 13: Basic Concepts of OOP in C++

C++ 13

The Display function

void MyVector::Display() {for(int i = 0; i < dim; i++)

std::cout << elem[i] << '\t';std::cout << std::endl;

} If the using namespace std;

directive is present, then std:: can be dropped.

Page 14: Basic Concepts of OOP in C++

C++ 14

The main function

int main() {int t[]={11, 22, 33, 44};using namespace MyVector;Init(t, 4); // if there is no using, then

// MyVector::InitSquareVect();Display();FreeVect();

}

Page 15: Basic Concepts of OOP in C++

C++ 15

The Address Operator and the Reference Type

Address operator (C and C++):& expression

where expression must be a modifiable lvalue.

In C this operator is often used when calling the scanf function.

Page 16: Basic Concepts of OOP in C++

C++ 16

Reference Type (C++)

In other words: alias name. We use the address operator Two variants:

type & name = data; or

type & formal_parameter type & is a new type (reference type).

Page 17: Basic Concepts of OOP in C++

C++ 17

Example (Reference Type)

int main() {int x[4] = {10, 20, 30, 40};int& y = x[0]; // y and x[0] are the sameint* z = x; // *z and x[0] are the samey = 50; // y, x[0] and *z changes*z = 60; // y, x[0] and *z changes

}

Page 18: Basic Concepts of OOP in C++

C++ 18

The Scope Operator

Possible forms:classname :: membernamespacename :: member:: name:: qualified_name

global scope

Page 19: Basic Concepts of OOP in C++

C++ 19

Global Scope

Use it to access the global variables. Example:

int x = 100;int main() {

char x = ’Z’;cout << x << endl; // local (character: ’Z’ )cout << ::x << endl; // global (integer: 100)

}

Page 20: Basic Concepts of OOP in C++

C++ 20

Example 2

#include <iostream>using namespace std;namespace X {

int x_var;namespace Y {

int x_var;}

}double x_var = 5.25;

Y: embedded namespace

global variable

Page 21: Basic Concepts of OOP in C++

C++ 21

Example 2 (main function)

int main() {char x_var = 'A';X::x_var = 10; // from the X namespaceX::Y::x_var = 20; // Y::x_var qualified namecout << x_var << endl; // local ('A’)cout << X::x_var << endl; // 10cout << ::x_var << endl; // global (5.25)cout << X::Y::x_var << endl; // 20

}

Page 22: Basic Concepts of OOP in C++

C++ 22

The Type Identifier Operator

typeid operator:typeid(typename)

ortypeid(expression)

Returns an object, which makes possible to find out the type of an expression in running time.

Page 23: Basic Concepts of OOP in C++

C++ 23

Using the typeid Operator

#include <iostream>#include <typeinfo.h>using namespace std;int main() {

cout << typeid( 97 ).name() << endl;cout << typeid( 97.0 ).name() << endl;cout << typeid( 97.0f ).name() << endl;cout << typeid( 'a' ).name() << endl;cout << typeid( static_cast<int>('a') ).name() << endl;

}

Page 24: Basic Concepts of OOP in C++

C++ 24

Output

intdoublefloatcharint We need the typeinfo.h header file in

case of using the typeid operator.

Page 25: Basic Concepts of OOP in C++

C++ 25

Simple Code in C++

#include <iostream>using namespace std;int main(){

cout << "Hello" << endl;}

Page 26: Basic Concepts of OOP in C++

C++ 26

Simple Code in Java

public class Hello { public static void main(String[] args)

{ System.out.println("Hello"); }}

Page 27: Basic Concepts of OOP in C++

C++ 27

Comparison

In Java there is no include or namespace (include and import are different).

In C++ main is a function, not a method. The list of formal parameters can be empty, if

we don’t want to use them. In C++ we use streams for input/output

opertations. The << (insertion) operator can be used with the standard cout object.

In Java the name of the public class must be the same as the file name. In C++ there is no such restriction.

Page 28: Basic Concepts of OOP in C++

C++ 28

Comparison

The string literals are written in the same way, but the type of "Hello"

in C++ is const char[6] in Java is an object of type String,

and thus it is composed of unicode characters.

Page 29: Basic Concepts of OOP in C++

C++ 29

Many Similarities

Comments Identifiers Keywords Literals

Page 30: Basic Concepts of OOP in C++

C++ 30

Java String and C++ string

We compare the Java String object with the C++ string “almost container”.

Differences: Java stores unicode, and C++ ASCII

characters the Java String is immutable in Java the concatenation (+) operator can

be used for each object, but in C++ only for two strings.

Page 31: Basic Concepts of OOP in C++

C++ 31

Java String and C++ string

For a C++ string object we can use the following operators: == != < > <= >=

In Javaban we use methods: equals, compareTo.

Substring: in C++ substr, and in Java substring.

Page 32: Basic Concepts of OOP in C++

C++ 32

Data Protection Using Modular Programming in C

We use static variables declared outside of functions.

One file contains all the data and the relevant code.

In the other file we can access the functions.

Page 33: Basic Concepts of OOP in C++

C++ 33

A Vector Module

Two files: MyVector.cpp MyMain.cpp

The two files must be in the same project.

Page 34: Basic Concepts of OOP in C++

C++ 34

MyVector.cpp

#include <iostream>using namespace std;

static int* elem;static int dim;

Page 35: Basic Concepts of OOP in C++

C++ 35

The Init function

void Init(int *e, int d){

elem = new int[d];dim = d;for(int i=0; i < dim; i++)

elem[i] = e[i];}

Page 36: Basic Concepts of OOP in C++

C++ 36

The FreeVect and SquareVect functions

void FreeVect() {delete []elem;

}

void SquareVect() {for(int i = 0; i < dim; i++)

elem[i] *= elem[i];}

Page 37: Basic Concepts of OOP in C++

C++ 37

The Display function

void Display() {for(int i = 0; i < dim; i++)

cout << elem[i] << '\t';cout << endl;

}

Page 38: Basic Concepts of OOP in C++

C++ 38

MyMain.cpp

void Init(int *, int );void FreeVect();void SquareVect();void Display();

//extern int * elem;

Page 39: Basic Concepts of OOP in C++

C++ 39

The main function

int main() {int t[]={1, 2, 3, 4};Init(t, 4);SquareVect();//elem[1] = 100;Display();FreeVect();

}

Page 40: Basic Concepts of OOP in C++

C++ 40

Abstract Data Type

A structure with data and codestruct name {

// data// code

};

data membersmember functions

Page 41: Basic Concepts of OOP in C++

C++ 41

ADT

The declaration of member functions will be inside the structure, and the definition outside.

If the whole definition is inside, then the function will be inline (evaluates like a macro)

Page 42: Basic Concepts of OOP in C++

C++ 42

ADT MyVector

struct MyVector {int *elem;int dim;void Init(int *e, int d);void FreeVect();void SquareVect();void Display();

};

data members

member functions

Page 43: Basic Concepts of OOP in C++

C++ 43

Member function definitions

Definition of member functions just like in the case of namespaces.

Page 44: Basic Concepts of OOP in C++

C++ 44

The main function

int main() {int t[]={1, 3, 5, 7};MyVector v;v.Init(t, 4);v.SquareVect();v.elem[1] = 100; // no protectionv.Display();v.FreeVect();

}

Page 45: Basic Concepts of OOP in C++

C++ 45

The MyVector class

class MyVector {private:

int *elem;int dim;

public:MyVector(int *e, int d);~MyVector();void SquareVect();void Display();

};

Page 46: Basic Concepts of OOP in C++

C++ 46

Constructor

MyVector::MyVector(int *e, int d){

elem = new int[d];dim = d;for(int i=0; i < dim; i++)

elem[i] = e[i];}

Page 47: Basic Concepts of OOP in C++

C++ 47

Destructor

MyVector::~MyVector() {delete []elem;

}

Page 48: Basic Concepts of OOP in C++

C++ 48

The SquareVect and Display functions

void MyVector:: SquareVect() {for(int i = 0; i < dim; i++)

elem[i] *= elem[i];}

void MyVector::Display() {for(int i = 0; i < dim; i++)

cout << elem[i] << '\t';cout << endl;

}

Page 49: Basic Concepts of OOP in C++

C++ 49

The main function

int main() {int t[]={2, 4, 6, 8};MyVector v(t, 4);v.SquareVect();//v.elem[1] = 100;v.Display();

}


Top Related