heade">
Practical test tomorrow
This presentation is the property of its rightful owner.
Sponsored Links
1 / 17

Practical test tomorrow PowerPoint PPT Presentation


  • 83 Views
  • Uploaded on
  • Presentation posted in: General

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. Getting output onto the screen. include the <iostream.h> header file use cout <<

Download Presentation

Practical test tomorrow

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Practical test tomorrow

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


Getting output onto the screen

Getting output onto the screen

  • include the <iostream.h> header file

  • use cout <<

  • use additional << to combine multiple elements together eg

    cout << “you entered “ << x;

  • use endl to add newline

    cout << “you entered” << x << endl;


Getting input from the keyboard

Getting input from the keyboard

  • include the <iostream.h> header file

  • declare a variable of a suitable type

    eg int num_in;

  • use cin

    eg cin >> num_in;


Getting input from the keyboard1

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;

}


Using a switch statement

Using a switch statement

  • What variable are you going to switch on?

  • What cases will you respond to?

  • Can provide a default case


Using a switch statement1

Using a switch statement

  • Type the skeleton of the switch statement first and compile

  • then flesh it out with cases

  • remember the break statement at the end of each case


Using a switch statement2

Using a switch statement

switch(x)

{

case 1:

//statements here ...

break;

case 2:

//statements here ...

break;

default:

//statements here ...

break;

}


Using the switch statement

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;

}


Writing classes

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:

  • };


Writing classes1

Writing classes

class classname

{

private:

int x;

public:

void setX(int x_in);

int getX(void);

};


Writing classes2

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;}

    };


Writing classes3

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;

}


Writing classes4

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;

}


Writing functions

Writing functions

int main()

{

exam myExam;

myExam.setNumQuestions(5);

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

return 0;

}


Inheritance

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

    {

    };


Good luck for today

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.


Good luck for today1

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


  • Login