17
1 Practical test tomorrow Will involve writing a simple class instantiating objects other C++ constructs as practised in the lab sheets to date inheritance and or aggregation

1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

Embed Size (px)

Citation preview

Page 1: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

1

Practical test tomorrow

Will involve writing a simple class

instantiating objects

other C++ constructs as practised in

the lab sheets to date

inheritance and or aggregation

Page 2: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

2

Getting output onto the screen

include the <iostream.h> header file

use cout << use additional << to combine

multiple elements together egcout << “you entered “ << x;

use endl to add newlinecout << “you entered” << x << endl;

Page 3: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

3

Getting input from the keyboard

include the <iostream.h> header file

declare a variable of a suitable typeeg int num_in;

use cin eg cin >> num_in;

Page 4: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

4

Getting input from the keyboard

#include "stdafx.h"#include <iostream.h>

int main( ){

int x;cout << "please enter an integer" << endl;cin >> x;cout << "you entered " << x << endl;return 0;

}

Page 5: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

5

Using a switch statement

What variable are you going to switch on?

What cases will you respond to?Can provide a default case

Page 6: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

6

Using a switch statement

Type the skeleton of the switch statement first and compile

then flesh it out with casesremember the break statement

at the end of each case

Page 7: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

7

Using a switch statement

switch(x){case 1:

//statements here ...break;

case 2://statements here ...break;

default://statements here ...break;

}

Page 8: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

8

Using the switch statement

switch(x){case 1:

cout << "you entered 1" << endl;break;

case 2:cout << "you entered 2" << endl;break;

default:cout << "you didn't enter 1 or 2" << endl;break;

}

Page 9: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

9

Writing classes

Remember the notion of data hiding and encapsulation

attributes are usually declared private public set and get methods provided to control

access to the attributes

class classname { private: public: };

Page 10: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

10

Writing classes

class classname{private:

int x;public:

void setX(int x_in);int getX(void);

};

Page 11: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

11

Writing classes

Methods can either be written inline in the class definition...

class classname{private:

int x;public:

void setX(int x_in){x=x_in;}int getX(void){return x;}

};

Page 12: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

12

Writing classes

…or for longer methods, a separate method implementation is usually added after the main() function, using the :: scope resolution operator

class classname{private:

int x;public:

void setX(int x_in);int getX(void);

};

void classname::setX(int x_in){

x=x_in;}

int classname::getX(){

return x;}

Page 13: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

13

Writing classes

class exam

{

private:

int numQuestions;

public:

void setNumQuestions(int num_in);

int getNumQuestions();

};

void exam::setNumQuestions(int num_in){

numQuestions=num_in;}

int exam::getNumQuestions(){

return numQuestions;}

Page 14: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

14

Writing functions

int main(){

exam myExam;myExam.setNumQuestions(5);

cout << "there are " << myExam.getNumQuestions() << "questions in the exam"

<< endl;

return 0;}

Page 15: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

15

Inheritance

A class can inherit from another class if the ‘a kind of’ relationship is appropriate - eg a dog is a kind of animal.

Use the colon : operator to indicate the base class when declaring your derived class

class dog: public animal{…};

Page 16: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

16

Good Luck for today

Don’t panic

Be methodical - read the question carefully and plan your approach on paper

add the minimum framework for your (function, class, method, for-loop etc) and compile it before ‘fleshing it out’. If you run out of time, just leave the empty shell.

Page 17: 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance

17

Good Luck for today

Name variables and functions sensibly

Use whitespace to make code easy to read

Comment where useful but don’t comment things which are obvious