1 / 49

Basic Concepts of OOP in C++

Basic Concepts of OOP in C++. A Comparison of C++ and Java. Darvay Zsolt. 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. Outline.

Download Presentation

Basic Concepts of OOP in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Concepts of OOP in C++ A Comparison of C++ and Java Darvay Zsolt

  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 C++

  3. Outline • Data protection using modular programming in C • Abstract Data Types • Classes C++

  4. The Namespace and its Members • The namespace concept • Example • Accessing the members of the namespace 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 } 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 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. 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;. 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. C++

  10. //with using directive #include <iostream> using namespace std; void Print() { ... cout << endl; } //without using directive #include <iostream> void Print() { ... std::cout << std::endl; } The iostream Header File 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]; } 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]; } 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. 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(); } 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. 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). 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 } C++

  18. The Scope Operator • Possible forms: classname :: member namespacename :: member :: name :: qualified_name global scope 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) } C++

  20. #include <iostream> using namespace std; namespace X { int x_var; namespace Y { int x_var; } } double x_var = 5.25; Example 2 Y: embedded namespace global variable 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 } 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. 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; } C++

  24. Output int double float char int • We need thetypeinfo.hheader file in case of using the typeid operator. C++

  25. Simple Code in C++ #include <iostream> using namespace std; int main() { cout << "Hello"<< endl; } C++

  26. Simple Code in Java public class Hello { public static void main(String[] args) { System.out.println("Hello"); } } C++

  27. Comparison • In Java there is no includeor 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. 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. C++

  29. Many Similarities • Comments • Identifiers • Keywords • Literals C++

  30. Java String and C++ string • We compare the JavaString objectwith 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. 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. 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. C++

  33. A Vector Module • Two files: • MyVector.cpp • MyMain.cpp • The two files must be in the same project. C++

  34. MyVector.cpp #include<iostream> usingnamespace std; staticint* elem; staticint dim; C++

  35. The Init function void Init(int *e, int d) { elem = newint[d]; dim = d; for(int i=0; i < dim; i++) elem[i] = e[i]; } C++

  36. The FreeVect and SquareVect functions void FreeVect() { delete []elem; } void SquareVect() { for(int i = 0; i < dim; i++) elem[i] *= elem[i]; } C++

  37. The Display function void Display() { for(int i = 0; i < dim; i++) cout << elem[i] << '\t'; cout << endl; } C++

  38. MyMain.cpp void Init(int *, int ); void FreeVect(); void SquareVect(); void Display(); //extern int * elem; C++

  39. The main function int main() { int t[]={1, 2, 3, 4}; Init(t, 4); SquareVect(); //elem[1] = 100; Display(); FreeVect(); } C++

  40. Abstract Data Type • A structure with data and code struct name { // data // code }; data members member functions 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) 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 C++

  43. Member function definitions • Definition of member functions just like in the case of namespaces. 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(); } C++

  45. The MyVector class class MyVector { private: int *elem; int dim; public: MyVector(int *e, int d); ~MyVector(); void SquareVect(); void Display(); }; C++

  46. Constructor MyVector::MyVector(int *e, int d) { elem = newint[d]; dim = d; for(int i=0; i < dim; i++) elem[i] = e[i]; } C++

  47. Destructor MyVector::~MyVector() { delete []elem; } 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; } 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(); } C++

More Related