C89 vs C99 C11 Overview of the Lecture
Introduction to Object Oriented Part 1 – Brief Overview of C89 vs C99 vs C11 Programming in C++ C89 vs C99 Part I
C11 K. N. King: Appendix B Jan Faigl Part 2 – Object Oriented Programming (in C++) Part 1 – Brief Overview of C89 vs C99 vs Department of Computer Science Differences between C and C++ C11 Faculty of Electrical Engineering Czech Technical University in Prague Classes and Objects
Lecture 10 Constructor/Destructor BE5B99CPL – C Programming Language Example – Class Matrix
Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 1 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 2 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 3 / 49 C89 vs C99 C11 C89 vs C99 C11 C89 vs C99 C11 Differences between C89 and C99 Differences between C89 and C99 Differences between C89 and C99 – Additional Libraries
Bool type – C99 provides _Bool type and macros in stdbool.h Comments – In C99 we can use a line comment that begins with // Loops – C99 allows to declare control variable(s) in the first
Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 5 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 6 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 7 / 49 C89 vs C99 C11 C89 vs C99 C11 C89 vs C99 C11 Overview of Changes in C11 – 1/2 Overview of Changes in C11 – 2/2 Generic Selection In C11, we can use a generic macros, i.e., macros with results that can be computed according to type of the pass variable (expression) Unicode support –
C C++ C C++ C was developed by Dennis Developed by Bjarne Stroustrup Concept of virtual functions is C++ offers the facility of using Ritchie (1969–1973) at AT&T in 1979 with C++’s predecessor “C not present in C virtual functions Part II Bell Labs with Classes” No operator overloading C++ allows operator overloading C is a procedural (aka C++ is procedural but also an Data can be easily accessed by Data can be put inside objects, Part 2 – Introduction to Object Oriented structural) programming object oriented programming other external functions which provides better data security language language C is a middle level language C++ is a high level language Programming C is a subset of C++ C++ can run most of C code The solution is achieved through C++ can model the whole solution C programs are divided into C++ programs are divided into a sequence of procedures or in terms of objects and that can modules and procedures classes and functions steps make the solution better organized
C is a function driven C++ is an object driven language C programs use top-down C++ programs use bottom-up language approach approach
Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 12 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 14 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 15 / 49 Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix
C C++ Objects Oriented Programming (OOP) C C++ Provides malloc() (calloc()) C++ provides new operator for Does not provide namespaces Namespaces are available for dynamic memory allocation memory allocation OOP is a way how to design a program to fulfill requirements Exception handling is not easy Exception handling through Try and make the sources easy maintain. It provides free() function for It provides delete and (delete[]) in C and Catch block memory de-allocation operator for memory de-allocation Inheritance is not possible Inheritance is possible Abstraction – concepts (templates) are organized into classes Does not support for virtual and C++ supports virtual and friend Objects are instances of the classes Function overloading is not Function overloading is possible friend functions functions possible (i.e., functions with the same name) Encapsulation Polymorphism is not possible C++ offers polymorphism Functions are used for Objects (streams) can be use for Object has its state hidden and provides interface to communicate C supports only built-in data It supports both built-in and input/output, e.g., scanf() and input/output, e.g., std::cin and with other objects by sending messages (function/method calls) types user-defined data types printf() std::cout Inheritance Mapping between data and In C++ data and functions are Does not support reference Supports reference variables, Hierarchy (of concepts) with common (general) properties that are functions is difficult in C easily mapped through objects variables using & further specialized in the derived classes Does not support definition C++ supports definition C programs are saved in files C++ programs are saved in files Polymorphism (overloading) operators (overloading) of the operators with extension .c with extension .cc, .cxx or .cpp An object with some interface could replace another object with http://techwelkin.com/difference-between-c-and-c-plus-plus the same interface
Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 16 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 17 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 19 / 49 Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Class Object Structure Creating an Object – Class Constructor Describes a set of objects – it is a model of the objects and defines: A class instance (object) is created by calling a constructor to The value of the object is structured, i.e., it consists of particular initialize values of the instance variables Interface – parts that are accessible // header file - definition of values of the object data fields which can be of different data type Implicit/default one exists if not specified the class type Heterogeneous data structure unlike an array from outside public, protected, private class MyClass { The name of the constructor is identical to the name of the class public: Object is an abstraction of the memory where particular values are Class definition Class implementation Body – implementation of the interface /// public read only stored class MyClass { MyClass::MyClass(int i) : _i(i) (methods) that determine the ability of int getValue(void) const; public: { Data fields are called attributes or instance variables the objects of the class private: // constructor _ii = i * i; /// hidden data field Data fields have their names and can marked as hidden or MyClass(int i); _d = 0.0; Instance vs class methods /// it is object variable MyClass(int i, double d); } accessible in the class definition // overloading constructor Data Fields – attributes as basic and int myData; }; Following the encapsulation they are usually hidden private: MyClass::MyClass(int i, double d) : _i(i) complex data types and structures const int _i; { Object: int _ii; _ii = i * i; (objects) Object composition // source file - implementation Instance of the class – can be created as a variable declaration or double _d; _d = d; Instance variables – define the state of the of the methods }; } int MyClass::getValue(void) const by dynamic allocation using the new operator { object of the particular class { MyClass myObject(10);//create an object as an instance of MyClass Class variables – common for all instances return myData; Access to the attributes or methods is using . or -> (for pointers }// at the end of the block, the object is destroyed } of the particular class to an object) MyClass *myObject = new MyClass(20, 2.3);//dynamic object creation delete myObject;//dynamic object has to be explicitly destroyed Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 20 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 21 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 22 / 49 Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Relationship between Objects Access Modifiers Constructor and Destructor
Access modifiers allows to implement encapsulation (information Objects may contain other objects hidding) by specifying which class members are private and which Constructor provides the way how to initialize the object, i.e., are public: allocate resources Object aggregation / composition public: – any class can refer to the field or call the method Programming idiom – Resource acquisition is initialization (RAII) Class definition can be based on an existing class definition – so, protected: – only the current class and subclasses (derived Destructor is called at the end of the object life there is a relationship between classes classes) of this class have access to the field or method It is responsible for a proper cleanup of the object Base class (super class) and the derived class private: – only the current class has the access to the field or Releasing resources, e.g., freeing allocated memory, closing files The relationship is transfered to the respective objects as instances method of the classes Destructor is a method specified by a programmer similarly to a By that, we can cast objects of the derived class to class instances of ancestor Access constructor Modifier Class Derived Class “World” However, unlike constructor, only single destructor can be specified Objects communicate between each other using methods !!! public The name of the destructor is the same as the name of the class (interface) that is accessible to them !! protected but it starts with the character as a prefix private ! ∼
Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 23 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 24 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 26 / 49 Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Constructor Overloading Example – Constructor Calling 1/3 Example – Constructor Calling 2/3 An example of constructor for creating an instance of the complex We can create a dedicated initialization method that is called from number different constructors Or we can utilize default values of the arguments that is combined In an object initialization, we may specify only real part or both with initializer list here the real and imaginary part class Complex { class Complex { class Complex { public: public: public: Complex(double r, double i) { init(r, i); } Complex(double r = 0.0, double i = 0.0) : re(r), im(i) {} Complex(double r) Complex(double r) { init(r, 0.0); } private: { Complex() { init(0.0, 0.0); } double re; re = r; double im; } private: }; Complex(double r, double i) void init(double r, double i) int main(void) { { re = r; re = r; { im = i; im = i; Complex c1; } } Complex c2(1.); ~Complex() {/* nothing to do in destructor*/} Complex c3(1., -1.); private: private: return 0; double re; double re; } double im; double im; }; }; Both constructors shared the duplicate code, which we like to avoid! Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 27 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 28 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 29 / 49 Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Example – Constructor Calling 3/3 Constructor Summary Class as an Extended Data Type with Encapsulation Data hidding is utilized to encapsulate implementation of matrix The name is identical to the class name class Matrix { Alternatively, in C++11, we can use delegating constructor The constructor does not have return value private: const int ROWS; Not even void const int COLS; class Complex { double *vals; public: Its execution can be prematurely terminated by calling return }; 1D array is utilized to have a continuous memory. Complex(double r, double i) It can have parameters similarly as any other method (function) { 2D dynamic array can be used in C++11. re = r; We can call other functions, but they should not rely on initialized In the example, it is shown im = i; How initialize and free required memory in constructor and } object that is being done in the constructor destructor Complex(double r) : Complex(r, 0.0) {} Constructor is usually public Complex() : Complex(0.0, 0.0) {} How to report an error using exception and try-catch statement (private) constructor can be used, e.g., for: How to use references private: double re; Classes with only class methods How to define a copy constructor double im; Prohibition to instantiate class How to define (overload) an operator for our class and objects }; Classes with only constants How to use C function and header files in C++ The so called singletons How to print to standard output and stream E.g., “object factories” How to define stream operator for output How to define assignment operator
Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 30 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 31 / 49 Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 33 / 49 Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Differences between C and C++ Classes and Objects Constructor/Destructor Example – Class Matrix Example – Class Matrix – Constructor Example – Class Matrix – Hidding Data Fields Example – Class Matrix – Using Reference Primarily we aim to hide direct access to the particular data fields Class Matrix encapsulate dimension of the matrix The at() method can be used to fill the matrix randomly For dimensions we provide the so-called “accessor” methods The function is defined in , but in C++ we Dimensions are fixed for the entire life of the object (const) random()
C89 vs C99 vs C11 – a brief overview of the changes C vs C++ – a brief overview of differences Object oriented programming in C++ Introduction to OOP Classes and objects Constructor Examples of C++ constructs Overloading constructors References vs pointers Data hidding – getters/setters Exception handling Operator definition Stream based output
Next: OOP – Polymorphism, inheritance, and virtual methods.
Jan Faigl, 2016 BE5B99CPL – Lecture 10: OOP in C++ (Part 1) 49 / 49