Basic Concepts of OOP in C++ Darvay Zsolt. C++ 2 Outline  The namespace and its members  The using declaration and directive  The address operator.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Introduction to C++ Programming
Introduction to C++ Systems Programming.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Working with Strings Lecture 2 Hartmut Kaiser
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Exception Handling. C++ 2 Outline  Throwing and handling exceptions  Exceptions of different types  The new operator and the exceptions  Re-throwing.
Object Oriented Programming (OOP) Lecture No. 11.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
Classes class A { ... };.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
A Sample Program #include using namespace std; int main(void) { cout
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Chapter 15 - C++ As A "Better C"
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
Chapter 1.2 Introduction to C++ Programming
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to C++ Systems Programming.
CSC1201: Programming Language 2
An Introduction to Java – Part I, language basics
Operator Overloading; String and Array Objects
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Variables, Identifiers, Assignments, Input/Output
Introduction to C++ Programming
Seoul National University
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 15 - C++ As A "Better C"
CSC1201: Programming Language 2
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS 144 Advanced C++ Programming February 21 Class Meeting
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Seoul National University
Presentation transcript:

Basic Concepts of OOP in C++ Darvay Zsolt

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

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 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;  Thendim = 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 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; }

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

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 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; }

C++ 24 Output int double float char int  We need the typeinfo.h header file in case of using the typeid operator.

C++ 25 Simple Code in C++ #include 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 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.

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 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.

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 using namespace std; static int* elem; static int dim;

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]; }

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 = new int[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(); }