490 likes | 582 Vues
Explore the basics of OOP in C++ and compare it with Java. Learn about namespaces, data protection, classes, and more with code examples.
E N D
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 C++
Outline • Data protection using modular programming in C • Abstract Data Types • Classes C++
The Namespace and its Members • The namespace concept • Example • Accessing the members of the namespace C++
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++
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++
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++
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++
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++
//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++
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++
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++
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++
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++
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++
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++
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++
The Scope Operator • Possible forms: classname :: member namespacename :: member :: name :: qualified_name global scope C++
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++
#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++
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++
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++
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++
Output int double float char int • We need thetypeinfo.hheader file in case of using the typeid operator. C++
Simple Code in C++ #include <iostream> using namespace std; int main() { cout << "Hello"<< endl; } C++
Simple Code in Java public class Hello { public static void main(String[] args) { System.out.println("Hello"); } } C++
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++
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++
Many Similarities • Comments • Identifiers • Keywords • Literals C++
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++
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++
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++
A Vector Module • Two files: • MyVector.cpp • MyMain.cpp • The two files must be in the same project. C++
MyVector.cpp #include<iostream> usingnamespace std; staticint* elem; staticint dim; C++
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++
The FreeVect and SquareVect functions void FreeVect() { delete []elem; } void SquareVect() { for(int i = 0; i < dim; i++) elem[i] *= elem[i]; } C++
The Display function void Display() { for(int i = 0; i < dim; i++) cout << elem[i] << '\t'; cout << endl; } C++
MyMain.cpp void Init(int *, int ); void FreeVect(); void SquareVect(); void Display(); //extern int * elem; C++
The main function int main() { int t[]={1, 2, 3, 4}; Init(t, 4); SquareVect(); //elem[1] = 100; Display(); FreeVect(); } C++
Abstract Data Type • A structure with data and code struct name { // data // code }; data members member functions C++
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++
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++
Member function definitions • Definition of member functions just like in the case of namespaces. C++
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++
The MyVector class class MyVector { private: int *elem; int dim; public: MyVector(int *e, int d); ~MyVector(); void SquareVect(); void Display(); }; C++
Constructor MyVector::MyVector(int *e, int d) { elem = newint[d]; dim = d; for(int i=0; i < dim; i++) elem[i] = e[i]; } C++
Destructor MyVector::~MyVector() { delete []elem; } C++
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++
The main function int main() { int t[]={2, 4, 6, 8}; MyVector v(t, 4); v.SquareVect(); //v.elem[1] = 100; v.Display(); } C++