Home Robotics C++ Physics II AP Physics B Electronics AP Java Astronomy Independent Study Summer Session Contests  About
                                                       

Example 1

 

Part 1: Fill in the Blanks

 

 

  The three classes of languages are

        _____________________________________________________  machine language

       _____________________________________________________  assembly language

       _____________________________________________________  high-level language

 

  Every C++ program begins execution at the function _____________ main

 

  The following escape sequence, when output with cout and the stream insertion operator, causes the cursor to position to the beginning of the next

    line on the screen.  /n 

 

  The program that translates the language in a C++ program into machine language is called a ______________________ compiler

 

  Single line comments begin with ________________ //

 

  Preprocessor directives begin with _________________ #

 

  A house is to a blueprint as a (an) ______________________ is to a class.  Object

 

  Every clas definition contains keyword _______________ immediately followed by the name of the class.  class

 

  Each parameter in a function header should specify both a(an)  ______________  and a(an) ________________________.  type   name

 

  When an object of a class maintains its own copy of an attribute, the variable that represents the attribute is known as a(an) ______________________

     data member

 

  Function ________________ from the <string> library reads characters until a

     newline character is encountered, then copies those characters into the

     specified string.  getline ().

 

  When a member function is defined outside the class definition, the function

     header must include the class name and the _______________, followed by the

     function name.  scope resolution operator or ::

 

  It is customary to define a class in a header file that has a _____________ file

     extension.  .h

 

  A function protype contains the function’s name, its return type and

     _______________________.  number, type, and order of parameters received.

 

  A fundamental principle of good software design is to separate interface from

     ____________________________ implementation

 

  CPU stands for ____________________________________ central processing unit

 


Part 2: True – False

Indicate whether the following statements are true or false by placing a check mark (√) in the

appropriate column.

  

 

True

False

All variables must be given a type when they are declared

ü

 

All variables must be declared before they are used

ü

 

Declarations can appear almost anywhere in the body of a C++ function

ü

 

When applied to integers, the modulus operator will return the quotient

 

ü

/  instructs the compiler to ignore code to end of the line

 

ü

Preprocessor directives end with a semicolon

 

ü

White space characters in strings are ignored by the compiler

 

ü

C++ is case sensitive

ü

 

When a new value is placed in a memory location, the old value is destroyed

ü

 

When a value is read from memory, the value originally in the memory location is destroyed

 

ü

All functions must be enclosed by braces

ü

 

C++ evaluates equal precedence arithmetic functions right to left

 

ü

Parentheses can be used to force the order of evaluation

ü

 

cin << is used to read input from the keyboard

 

ü

A constructor that takes no arguments is called a default constructor

ü

 

A class interface describes the public member functions

ü

 

A class interface describes what services a user can request

ü

 

A class interface does not describe how requests are carried out

ü

 

The driver file has a .h extension

 

ü

The implementation file has a .h extension

 

ü

public is a keyword – it is an access specifier

ü

 

You can use the return keyword in a function declared to have return type void

 

ü


 

Part 3: Analyze Code

  Write the output of the following code in the space following the code

 

#include<iostream.h>

 

int main ()

{

      cout <<"This is an";

      cout <<" "<<"illustration of";

      cout <<" output in C++"<<endl;

      return 0;

}

 

This is an illustration of output in C++

 

  Write the output of the following code in the space following the code

 

#include <iostream.h>

int main ()

{

      int a = 10;

      int b = 20;

      if (a = b)

            cout <<"This prints"<<endl;

      if (b > a)

            cout <<"This prints also"<<endl;

      return 0;

}

 

 This prints


Part 4a: Write Code Segments

Given that you have a class named Mosquito. The class has a parameterless constructor and a constructor that accepts an integer parameter. It also has a public function named walk. The walk function takes an integer parameter.

 

  Write a line of code in the main function that will create an appropriately

     named object of the Mosquito class that calls the parameterless constructor.

 

            Mosquito biter ();

 

  Write code that will use the object above to call the walk function and pass it

     an appropriate parameter.

 

             int x = 5;

             biter.walk(x);

 

Part 4b. Write a Complete Class

Write a class named Banking (interface separate from implementation) that has

 

  Two data members:

       An integer named customerID

       A double named deposit

  Two constructor functions:

       A parameterless constructor that sets the private data members to 0

       A constructor that takes 2 parameters, an integer and a double

  A third function named status that returns the value of the deposit.

 

Place the interface here.

 

class Banking                    //Assume this file named banking.h

{

      public:                            //Constructors and member functions declared public

            Banking ();

            Banking (int member, double amountIn);

            double Status (double amount);

      private:                        //Data members declared private

            int customerID;

            double deposit;

};

     

 

 

 

Place the definition here

 

#include <iostream.h>           < > indicate a standard C++ header file

#include “banking.h”             //Quotations indicate a user written header file

 

Banking::Banking()               //Note the scope resolution operator (:: and why it is used)

{

      customerID = 0;

      deposit = 0.0;

}

Banking::Banking(int custNumber, double custDeposit)

{

      customerID = custNumber;

      deposit = custDeposit;

}

Banking::Status ()

{

    return deposit;

}


 

NOTE: A DRIVER WAS NOT REQUIRED - ONE IS PROVIDED BELOW

#include <iostream.h>

#include "banking.h"

 

int main ()

{

    int memberNumber = 0;

    double deposit = 0;

    cout <<"What is your member number?"<<endl;

    cin >> memberNumber;

    cout << "How much do you wish to deposit?<<endl;

    cin >> deposit

    Banking myBank (memberNumber, deposit)     //Creating an object and calling constructor

    cout <<"Your deposit was: "<<myBank.status<<endl;   //Printing amount of deposit

    return 0;

}

 

 

Part 5: Extra credit

 

Identify the following contributors to the field of Computer Science or computers.

First and last names must be given to receive credit.

 

Father of computer science:                            ________________  Alan Turing

 

Wrote the first program:                                     ________________  Agusta Byron

 

Wrote the first compiler:                                    ________________  Grace  Murray Hopper

 

Designed the ENIAC:                                           ________________  John Mauchley

 

Developed the stored-program concept:    ________________  John von Neumann

 

Developed the first computer:                         ________________  Charles Babbage 

 

The Boolean data type is named after him: ________________  George Boole

 

Developed the transistor:                               __________________ William Shockley