Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.

Similar presentations


Presentation on theme: "Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation."— Presentation transcript:

1 Introduction to C++

2 Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation Creating objects

3 C++? Developed by Bjarne Stroustrup C with extentions Object orientation (classes, inheritance, …) Templates References (also as parameter) New (and some better) libraries (,, …) - Operator overloading - … Can be used mixed with C (not recommended) Well-suited to big programming projects

4 What are references? “An alternative name for an object” An object that shares the data with another object Can only be used on places where the object could also be used void main() { float x = 1.0; float &refx = x;//shares dataspace with x float y = refx;// y has now value of x and refx refx = 15.0; //set 15.0 in dataspace of x }

5 What are references? Can be used to avoid pointers Handy in functions void swap (int &a, int &b) { //references int temp = a; a = b; b = temp; } Work in actual dataspace of variables NOT a pointer!! References to the same object in his whole scope

6 Object orientation Work with elements/objects (Car, Robot, …) instead of loose variables C++ program has Main loop (multiple) Classes, objects, … Classes (blueprint of an object) Object (instance of a class, has a reserved dataspace)

7 Class Blueprint of an object, describes what an object can and stores Define member functions and data members A structure with functions (but default private access rules, see later)

8 Class class Gradebook{ public: /*Public member functions of the class*/ Gradebook(){ cout << "Enter the name: " ; getline(cin,Owner); } void PrintName(){ cout << "Name of owner of the Gradebook is: " << GetOwner() <<endl; } void SetOwner(string name){ cout << "Setting owner..." << endl; Owner = name; } //accessor: Get the name of the owner string GetOwner(){ return Owner; } private: /*Private data members of the class */ string Owner; }; “class” to start a class Access specifier Member functions Data member Constructor

9 Access specifiers Define the accessability of member functions and data members Public Default for structures in C++ Can be called from outside the class Available to “the public” No data hidding Used for most member functions Private Default for classes Only accessable from inside the class Data is hidden, data hiding Used for data members or internal member functions Make accessable by using mutators/accessors (public functions to manipulate data members)

10 Constructor/Destructor Constructor Always called when object is created Same name as class No return type, not even “void” If none defined it is created by the compiler Default without arguments Can be overloaded

11 Constructor/Destructor //Constructor with argument Gradebook(string name){ Owner = name; } //Constructor without arguments Gradebook(){ cout << "Enter the name: " ; getline(cin,Owner); }

12 Constructor/Destructor Destructor Always called when object is deleted Same name as class but “~” in front Used to clean up memory (linked lists, …), close files, … Created by compiler if not defined

13 Constructor/Destructor ~Gradebook(){ cout << "Calling destructor of the Gradebook class..." << endl; }

14 Interface-Implementation separation Make your classes reusable Define in separate files Interface Only declarations of functions Data members Implementation Actual function code Use “classname::functionname”

15 Interface /* Gradebook.h: Interface of the Gradebook class */ class Gradebook{ public: /*Public member functions of the class*/ //Constructors Gradebook(string name); Gradebook(); //destructor ~Gradebook(); void PrintName(); //mutator void SetOwner(string name); //accessor string GetOwner(); private: /*Private data members of the class */ string Owner; //owners name };

16 Implementation /* Gradebook.cpp*/ Gradebook::Gradebook(string name){ Owner = name; } Gradebook::Gradebook(){ cout << "Enter the name: " ; getline(cin,Owner); } void Gradebook::PrintName(){ cout << "Name of owner of the Gradebook is: " << GetOwner() <<endl; } void Gradebook::SetOwner(string name){ cout << "Setting owner..." << endl; Owner = name; } string Gradebook::GetOwner(){ return Owner; } //destructor Gradebook::~Gradebook(){ cout << "Calling destructor of the Gradebook class..." << endl; }

17 Creation of objects Automatic class object Object is created like any other variable (Gradebook G1;) Memory allocated on the stack Deleted when the scope ends Using new-operator Memory dynamic allocated Result is pointer to allocated memory (Gradebook *G2 = new Gradebook();) Need to use delete-operator to free this memory (comparable to malloc-free in C)

18 Example

19 References C++ for Programmers by Paul J. Deitel The C++ Programming Language: Special Edition by Bjarne Stroustrup

20 Questions?


Download ppt "Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation."

Similar presentations


Ads by Google