Download presentation
Presentation is loading. Please wait.
Published byBrooke Tyler Modified over 9 years ago
1
Basic Concepts of OOP in C++ Darvay Zsolt
2
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
3
C++ 3 Outline Data protection using modular programming in C Abstract Data Types Classes
4
C++ 4 The Namespace and its Members The namespace concept Example Accessing the members of the namespace
5
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 }
6
C++ 6 Example #include namespace MyVector { int *elem; int dim; void Init(int *e, int d); void FreeVect(); void SquareVect(); void Display(); } A vector namespace with integer elements
7
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.
8
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; Thendim = 14; is the same as MyVector::dim = 14;.
9
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.
10
C++ 10 The iostream Header File //with using directive #include using namespace std; void Print() {... cout << endl; } //without using directive #include void Print() {... std::cout << std::endl; }
11
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]; }
12
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]; }
13
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.
14
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::Init SquareVect(); Display(); FreeVect(); }
15
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.
16
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).
17
C++ 17 Example (Reference Type) int main() { int x[4] = {10, 20, 30, 40}; int& y = x[0]; // y and x[0] are the same int* z = x; // *z and x[0] are the same y = 50; // y, x[0] and *z changes *z = 60; // y, x[0] and *z changes }
18
C++ 18 The Scope Operator Possible forms: classname :: member namespacename :: member :: name :: qualified_name global scope
19
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) }
20
C++ 20 Example 2 #include using namespace std; namespace X { int x_var; namespace Y { int x_var; } double x_var = 5.25; Y: embedded namespace global variable
21
C++ 21 Example 2 (main function) int main() { char x_var = 'A'; X::x_var = 10; // from the X namespace X::Y::x_var = 20; // Y::x_var qualified name cout << x_var << endl; // local ('A’) cout << X::x_var << endl; // 10 cout << ::x_var << endl; // global (5.25) cout << X::Y::x_var << endl; // 20 }
22
C++ 22 The Type Identifier Operator typeid operator: typeid(typename) or typeid(expression) Returns an object, which makes possible to find out the type of an expression in running time.
23
C++ 23 Using the typeid Operator #include 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 ('a') ).name() << endl; }
24
C++ 24 Output int double float char int We need the typeinfo.h header file in case of using the typeid operator.
25
C++ 25 Simple Code in C++ #include using namespace std; int main() { cout << "Hello" << endl; }
26
C++ 26 Simple Code in Java public class Hello { public static void main(String[] args) { System.out.println("Hello"); }
27
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.
28
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.
29
C++ 29 Many Similarities Comments Identifiers Keywords Literals
30
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.
31
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.
32
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.
33
C++ 33 A Vector Module Two files: MyVector.cpp MyMain.cpp The two files must be in the same project.
34
C++ 34 MyVector.cpp #include using namespace std; static int* elem; static int dim;
35
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]; }
36
C++ 36 The FreeVect and SquareVect functions void FreeVect() { delete []elem; } void SquareVect() { for(int i = 0; i < dim; i++) elem[i] *= elem[i]; }
37
C++ 37 The Display function void Display() { for(int i = 0; i < dim; i++) cout << elem[i] << '\t'; cout << endl; }
38
C++ 38 MyMain.cpp void Init(int *, int ); void FreeVect(); void SquareVect(); void Display(); //extern int * elem;
39
C++ 39 The main function int main() { int t[]={1, 2, 3, 4}; Init(t, 4); SquareVect(); //elem[1] = 100; Display(); FreeVect(); }
40
C++ 40 Abstract Data Type A structure with data and code struct name { // data // code }; data members member functions
41
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)
42
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
43
C++ 43 Member function definitions Definition of member functions just like in the case of namespaces.
44
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 protection v.Display(); v.FreeVect(); }
45
C++ 45 The MyVector class class MyVector { private: int *elem; int dim; public: MyVector(int *e, int d); ~MyVector(); void SquareVect(); void Display(); };
46
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]; }
47
C++ 47 Destructor MyVector::~MyVector() { delete []elem; }
48
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; }
49
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(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.