Download presentation
Presentation is loading. Please wait.
Published byAshley Jessica Ray Modified over 9 years ago
1
Week 14 - Monday
2
What did we talk about last time? Introduction to C++ Input and output Functions Overloadable Default parameters Pass by reference
6
Let's write a complete C++ program that reads in: A string An integer Then, it prints out the string however many times the integer specified
8
When you want to dynamically allocate memory in C++, you use new (instead of malloc() ) No cast needed It "feels" a lot like Java int* value = new int(); //make an int int* array = new int[100]; //array of ints Wombat* wombat = new Wombat(); //make a Wombat Wombat* zoo = new Wombat[100]; //makes 100 Wombats with the default constructor int* value = new int(); //make an int int* array = new int[100]; //array of ints Wombat* wombat = new Wombat(); //make a Wombat Wombat* zoo = new Wombat[100]; //makes 100 Wombats with the default constructor
9
When you want to free dynamically allocated memory in C++, use delete (instead of free() ) If an array was allocated, you have to use delete[] int* value = new int(); //make an int delete value; Wombat* wombat = new Wombat(); delete wombat; Wombat* zoo = new Wombat[100]; delete[] zoo; //array delete needed int* value = new int(); //make an int delete value; Wombat* wombat = new Wombat(); delete wombat; Wombat* zoo = new Wombat[100]; delete[] zoo; //array delete needed
10
You can compile C code with C++ Weird things can happen, but we aren't going to going into those subtle issues However, you now know and love the standard C libraries You can use them in C++ too You just have to include different header files C Library Header C++ EquivalentPurpose ctype.hcctype Character manipulation limits.hclimits Constants for integer limits math.hcmath Math functions stdio.hcstdio C I/O functions stdlib.hcstdlib Random values, conversion, allocation string.hcstring Null-terminated string manipulation time.hctime Time functions
11
A struct in C++ is actually just a class where all the members are public You can even put methods in a struct in C++ Otherwise, it looks pretty similar You don't have to use the struct keyword when declaring struct variables Except in cases when it is needed for disambiguation
12
Here's a TreeNode struct in C++ Write a tree insertion with the following signature struct TreeNode { int value; TreeNode* left; TreeNode* right; }; struct TreeNode { int value; TreeNode* left; TreeNode* right; }; void insert(TreeNode* &root, int data);
14
Let's see how objects work in C++ by looking at classically important elements of OOP Encapsulation Dynamic dispatch Polymorphism Inheritance Self-reference
15
Information hiding We want to bind operations and data tightly together Consequently, we don't want you to touch our privates Encapsulation in C++ is provided by the private and protected keywords Unlike Java, you mark sections as public, private, or protected, not individual members and methods Hardcore OOP people think that all data should be private and most methods should be public
16
class A { private: int a; public: int getA() { return a; } void setA(int value) { a = value; } }; class A { private: int a; public: int getA() { return a; } void setA(int value) { a = value; } };
17
Allows code reuse Is thought of as an "is-a" relationship C++ allows multiple inheritance, but you should only use it if you know what you're doing, usually as part of a design pattern Deriving a subclass usually means creating a "refined" or "more specific" version of a superclass
18
class B : public A { //has member and methods from A }; class C : public A { private: //has A stuff and more int c; public: int getC(){ return c; } void increment() { c++; } }; class B : public A { //has member and methods from A }; class C : public A { private: //has A stuff and more int c; public: int getC(){ return c; } void increment() { c++; } };
19
A confusing word whose underlying concept many programmers misunderstand Polymorphism is when code is designed for a superclass but can be used with a subclass If AudiRS5 is a subtype of Car, then you can use an AudiRS5 where you could use a Car
20
void drive( Car* c ); //defined somewhere … class AudiRS5 : public Car {}; … Car car; AudiRS5 audi; drive( &audi ); //okay drive( &car ); //okay void drive( Car* c ); //defined somewhere … class AudiRS5 : public Car {}; … Car car; AudiRS5 audi; drive( &audi ); //okay drive( &car ); //okay
21
Polymorphism can be used to extend the functionality of an existing method using dynamic dispatch In dynamic dispatch, the method that is actually called is not known until run time
22
class A { public: virtual void print() { cout << "A";} }; class B : public A { public: void print() { cout << "B";} }; class A { public: virtual void print() { cout << "A";} }; class B : public A { public: void print() { cout << "B";} };
23
A a; B b; A* p; a.print(); // A b.print(); // B p = &a; p->print(); // A p = &b; p->print(); // B A a; B b; A* p; a.print(); // A b.print(); // B p = &a; p->print(); // A p = &b; p->print(); // B
24
Objects are able to refer to themselves This can be used to explicitly reference variables in the class Or, it can be used to provide the object itself as an argument to other methods Self-reference in C++ is provided in part through the this keyword this is a pointer to the object you're inside of
25
class Stuff { private: int things; public: void setThings(int things) { this->things = things; } }; class Stuff { private: int things; public: void setThings(int things) { this->things = things; } };
26
class SelfAdder { public: void addToList(List& list) { list.add(this); } }; class SelfAdder { public: void addToList(List& list) { list.add(this); } };
28
C++ madness Templates in C++
29
Keep working on Project 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.