SCC111: weeks 11-15 Flashcards
(14 cards)
what type of programming language is C++?
-procedural programming language
what is a procedural programming language?
-programs are executed in an order: sequentially
what are some key features of a procedural programming language?
-there is a well-defined starting point to the program
-code is broken down into manageable chunks (functions)
-variables are passed as parameters between functions
what is the benefit of using object orientated programming languages?
-encapsulates data
-variables belonging to an object are attributes
-functions belonging to an object are methods
-programmers can choose the visibility of data belonging to an object
how are classes defined in C++?
class Car{
};
//REMEMBER THE SEMICOLON
how would you define the class car with the variable colour and the method repaint?
class Car{
char* colour;
public:
void repaint(char* newColour);
};
void Car::repaint(char* newColour){
colour = newColour;
}
how would you create a new instance of a car class and call the repaint function on it?
Car myCar;
myCar.repaint((char*)”Red”);
how would you declare public and private methods and attributes?
class Car{
private:
int milesDriven = 0;
int getMilesDriven();
public:
char colour;
int repaint(char newColour);
};
how is a class constructor written?
class Car{
public:
Car();
};
Car::Car(){
//initialisation code here
}
can classes in C have multiple constructors??
yus
how are constructors differentiated between?
-different parameters needed for each one
what is a destructor?
function that is invoked automatically when an object is being destroyed.
what is the syntax for a destructor?
class BankAccount{
//attributes and methods
//destructor:
~BankAccount();
};
BankAccount::~BankAccount(){
//destructor code
}