Documente Academic
Documente Profesional
Documente Cultură
Department of Computing
Curtin University
1/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
2/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Outline
C++ Overview
Scope
Classes
Inheritance
Pointers
Templates
Overloading
3/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
C++ Overview
4/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Commonalities with C
5/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
6/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Filename Extensions
7/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Compiling
I Gcc can compile C++ as well as C:
[user@pc]$ gcc -c file1.cpp
8/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Namespaces
9/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
10/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Classes
11/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Class Syntax
This would appear in “person.hpp”:
class Person {
public:
Person(); // Constructor
Person(string inName, int inAge);
~Person(); // Destructor
private:
string name; // Private fields
int age;
};
12/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
15/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Stack-Based Objects
We can also allocate objects on the stack:
void func() {
Person p("Ralph", 7); // Constructs p
...
int age = p.getAge();
p.setAge(age + 1);
...
}
Destructors
17/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Static Methods
I C++ supports static fields and methods.
I A static field/method is shared by all instances of the class.
I This is same meaning as in Java (not C!)
I For example:
class Person {
public:
...
static void staticFunc(int param);
...
};
...
Person::staticFunc(10);
18/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
A Note on Java
I Java has no equivalent to the “.” operator in C/C++
(because you can’t have an object without a reference).
I Confusingly, Java uses the symbol “.” to replace “->” and
“::”.
19/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
20/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Inheritance
Classes can inherit/extend other classes, as follows:
class Employee : public Person {
public:
Employee();
Employee(string name, int age, int salary);
~Employee();
...
};
However:
I Classes don’t have to inherit from anything (unlike in Java).
I C++ supports multiple inheritance (use sparingly, if at all):
Virtual Methods
In C++, you can’t override a method unless it’s “virtual”:
class Person {
...
virtual bool equals(Person* p) const;
...
};
23/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
24/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
25/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
int main() {
int num1, num2;
27/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
int main() {
ofstream* file = new ofstream("file.txt");
delete file;
return 0;
}
28/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
int main() {
ofstream file("file.txt"); // Stack-based object
return 0;
}
References
I A C++ “reference” is a pointer that is automatically
dereferenced.
I When used, C++ automatically puts a * in front of it.
I References are set once (when declared). You can’t change
where they point.
int x = 10;
int& y = x; // y is a reference to integer x.
I Here, y is a reference to x.
I The & in the declaration means “reference-to”, not “address-of”.
I But what’s the point?
30/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Reference Parameters
I Functions (and methods) can take references as parameters:
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
...
int x = 10, y = 20;
swap(x, y);
31/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Auto Pointers
I “Auto pointers” are another special type of pointer.
I Only one auto pointer can point to a given object!
I If you try to copy the pointer, the original pointer will be
wiped.
I Auto pointers are implemented by the class “auto_ptr”.
#include <memory>
...
auto_ptr<Employee> ptr(new Employee());
32/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
33/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Templates
34/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Template Declarations
35/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Template Functions
A template function might look like this:
template<class T>
T square(T num) {
return num * num;
}
36/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
private:
T obj;
string password;
};
int a = s1->getObj("pass");
string b = s2->getObj("12345");
38/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
I C++’s templates and Java’s generics are not quite the same.
I Templates work by creating different copies for different types.
I Generics work by automatically inserting safe typecasts.
I They have similarities:
I Both let you re-use code for different data types.
I Both let you avoid gratuitous, unsafe typecasting.
I And differences:
I Templates apply to any data type; generics apply only to
objects.
I Templates allow compile-time calculations
(“metaprogramming”).
39/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
40/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Method/Function Overloading
I C++ allows for function and method overloading (like Java,
but unlike C).
I Multiple functions/methods can have the same name, but
different parameter lists.
I For example:
int readInt() {
...
}
Default Arguments
I C++ lets you set “default” values for function parameters:
int readInt(string prompt = "Enter a value: ") {
...
}
44/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
Wrapping Up
45/46
C++ Overview Scope Classes Inheritance Input and Output Pointers Templates Overloading
46/46