OOP - Topic 07 - Lecture - Introduction To C++
OOP - Topic 07 - Lecture - Introduction To C++
Mac Android
iOS etc….
High Performance Computing
(including games)
Digital Signal Large Scale
Processing Simulations
Graphics Real-time
Processing systems
Energy Efficient Computing
(including mobile apps)
• C++ programming languages are more energy efficient than others because they simply run
faster
• A program that can be executed faster reduces the load on a CPU, which in turn demands less
energy from the massive data centers that pull electricity from the grid.
Embedded Systems
(including device drivers)
Embedded PCs
Microcontroller
Toasters
RTOSes
Flexibility
C++ is a multi-paradigm
language:
•Object-oriented
programming
•Procedural programming
•Generic programming
Legacy C++ can utilize code written in C
and Fortran. They have
powerful C++ libraries: e.g.
Standard Template Library
(STL), QT GUI library, Eigen
Matrix Library for linear algebra
With great power comes great
responsibility
- François-Marie Arouet
Resource Management
#include interface
#include “Student.h”
string Student::getDescription ()
{ Definition/implementation in
return name + “ “ + id; source file (e.g. Student.cpp)
}
Encapsulation 2.
“Encapsulation is the mechanism that binds code and data together and
keeps them safe and away from outside inference and misuse.”
Box.h (Header file) Box.cpp (Implementation)
#include<iostream> #include “Box.h”
using namespace std; // constructor (implementation)
class Box{ Box::Box(int l, int b, int h){
private: length = l;
int length; breadth = b;
int breadth; height = h;
int height; }
public: //setter (implementation)
//constructor void Box::setLength(int l){
Box(int l, int b, int h); length = l;
//setter }
//getter (implementation)
void setLength(int l);
int Box::getLength(){
//getter
return length;
int getLength();
}
//calculate volume
//calculate volume (implementation)
int volume();
int Box::volume(){
return (length*breadth*height);
};
}
Main.cpp
#include<iostream>
using namespace std;
int main(){
//creating a Box object
Box *b1 = new Box(2,3,4);
//calculating and display the volume of Box b1
cout<<“\nThe volume of Box b1 is “ << b1->volume();
//change the length of Box b1
b1->setLength(10);
//calculating and display the volume of Box b1
cout<<“\nThe new volume of Box b1 is “ << b1->volume();
return 0;
}
Inheritance
class Rectangle : public Shape
{
public:
void draw ();
}; implicit override
Student& b = a;
Student b;
&b; &b returns the memory
address of Student b
private:
attributes (fields)
private methods
};
Implicit Methods
void function ()
{
MyClass A; Default Constructor
A = B; Assignment Operator
} Destructor (x 2)
The Standard C++ Library and
Standard Template Library
Everything in std namespace
#include <iostream>
[...]
std::cout << “Hello, World!” << std::endl;
OR
#include <iostream>
using namespace std;
[...]
cout << “Hello, World!” << endl;
Everything in std namespace
#include <string>
[...]
std::string name = “Anthony Hewish”;
OR
#include <string>
using namespace std;
[...]
string name = “Jocelyn Bell”;
C++ Basic I/O
C++ uses a convenient abstraction called streams to
perform input and output operations in sequential
media such as the screen, the keyboard or a file.
return 0;
}
Let’s review the differences
C++ is a low level and indeed
platform neutral programming
language
C# is a high level language that is component oriented
C++ allows any type to be passed
by value, reference or pointer
C# does not have the concept of function pointers
In C++ the memory that is allocated
in the heap dynamically has to be
explicitly deleted
In C#, memory management is automatically handled by
garbage collector
In C++, the end of the class
definition has a closing brace
followed by a semicolon
In C#, the end of the class definition has a closing brace alone.
C++ does not contain for each
statement (only having for, while
and do…while)
In C#, has another flow control statement called for each
C++ uses stream operators
to simplify file I/O
C++ templates
are more flexible and powerful than
C# generics
Review the similarities
C++ supports encapsulation,
inheritance, polymorphism
(and abstraction)
C++ and C# share
syntax based on C
Enjoy programming in C++!
This Week’s Tasks
Supplementary Exercise: C++ Classes and Inheritance
Credit Task 2: Student Grading System (C++ Implementation)
Supplementary Exercise: Planetary Rover UML Class Diagram
Supplementary Exercise: Planetary Rover Code