Homework 3. Due: Feb 25, 23:59pm. Section 1 (20 pts, 2 pts each) Multiple Choice Questions

Size: px
Start display at page:

Download "Homework 3. Due: Feb 25, 23:59pm. Section 1 (20 pts, 2 pts each) Multiple Choice Questions"

Transcription

1 Homework 3 Due: Feb 25, 23:59pm Section 1 (20 pts, 2 pts each) Multiple Choice Questions Q1: A function that modifies an array by using pointer arithmetic such as ++ptr to process every value of the array should have a parameter that is: a. A nonconstant pointer to nonconstant data. b. A nonconstant pointer to constant data. c. A constant pointer to nonconstant data. d. A constant pointer to constant data. ANS: a. A nonconstant pointer to nonconstant data. Q2: A function that prints a string by using pointer arithmetic such as ++ptr to output each character should have a parameter that is: a. A nonconstant pointer to nonconstant data. b. A nonconstant pointer to constant data. c. A constant pointer to nonconstant data. d. A constant pointer to constant data. ANS: b. A nonconstant pointer to constant data. Q3: Member access specifiers (public and private) can appear: a. In any order and multiple times. b. In any order (public first or private first) but not multiple times. c. In any order and multiple times, if they have brackets separating each type. d. Outside a class definition. ANS: a. In any order and multiple times. Q4: A default constructor: a. Is a constructor that must receive no arguments. b. Is the constructor generated by the compiler when no constructor is provided by the programmer. c. Does not perform any initialization. d. Both (a) and (b). ANS: d. Both (a) and (b). Q5: Which of the following is not true of a destructor? a. It performs termination housekeeping. b. It is called before the system reclaims the object s memory. c. If the programmer does not explicitly provide a destructor, the compiler creates an empty destructor. d. It releases the object s memory. ANS: d. It releases the object s memory. Q6: Given the class definition: class CreateDestroy

2 public: CreateDestroy() cout << "constructor called, "; } ~CreateDestroy() cout << "destructor called, "; } }; What will the following program output? int main() } CreateDestroy c1; CreateDestroy c2; return 0; a. constructor called, destructor called, constructor called, destructor called, b. constructor called, destructor called, c. constructor called, constructor called, d. constructor called, constructor called, destructor called, destructor called, ANS: d. constructor called, constructor called, destructor called, destructor called, Q7: The assignment operator (=) can be used to: a. Test for equality. b. Copy data from one object to another. c. Compare two objects. d. Copy a class. ANS: b. Copy data from one object to another. Q8: Increment::Increment( int c, int i ) : increment ( i ) count = c; } does not cause any compilation errors. This tells you that: a. count must be a non-const variable. b. count must be a const variable. c. increment must be a non-const variable. d. increment must be a const variable. ANS a. count must be a non-const variable. Q9: If the line: friend class A; appears in class B, and the line: friend class B; appears in class C, then: a. Class A is a friend of class C. b. Class A can access private variables of class B.

3 c. Class C can call class A s private member functions. d. Class B can access class A s private variables. ANS: b. Class A can access private variables of class B Q10: For a non-constant member function of class Test, the this pointer has type: a. const Test * b. Test * const c. Test const * d. const Test * const ANS: b. Test * const Section 2 Programming questions All the questions are in the required textbook: Question % // Exercise 9.8 Solution: Ex09_08.cpp #include <iostream> #include "Date.h" // include definitions of class Date using namespace std; int main() const unsigned int MAXDAYS = 16; Date d( 12, 24, 2013 ); // instantiate object d of class Date // output Date object d's value for ( unsigned int loop = 1; loop <= MAXDAYS; ++loop ) d.print(); // invokes function print

4 d.nextday(); // invokes function next day } // end for cout << endl; } // end main // Exercise 9.8 Solution: Date.h #ifndef DATE_H #define DATE_H class Date public: explicit Date( int = 1, int = 1, int = 2000 ); // default constructor void print(); // print function void setdate( int, int, int ); // set month, day, year void setmonth( int ); // set month void setday( int ); // set day void setyear( int ); // set year unsigned int getmonth() const; // get month unsigned int getday() const; // get day unsigned int getyear() const; // get year void nextday(); // next day private:

5 unsigned int month; unsigned int day; unsigned int year; bool leapyear() const; // leap year int monthdays() const; // days in month }; // end class Date #endif // Exercise 9.8 Solution: Date.cpp // Member-function definitions for class Date. #include <iostream> #include <stdexcept> #include <array> #include "Date.h" // include definition of class Date using namespace std; Date::Date( int m, int d, int y ) setdate( m, d, y ); // sets date } // end Date constructor void Date::setDate( int mo, int dy, int yr )

6 setmonth( mo ); // invokes function setmonth setday( dy ); // invokes function setday setyear( yr ); // invokes function setyear } // end function setdate void Date::setDay( int d ) if ( month == 2 && leapyear() && d <= 29 && d >= 1 ) day = d; else if ( d <= monthdays() && d >= 1 ) day = d; else throw invalid_argument( "day out of range for current month" ); } // end function setday void Date::setMonth( int m ) if ( m <= 12 && m >= 1 ) month = m; else throw invalid_argument( "month invalid" ); } // end function setmonth void Date::setYear( int y )

7 if ( y >= 2000 ) year = y; else throw invalid_argument( "year invalid" ); } // end function setyear unsigned int Date::getDay() const return day; } // end function getday unsigned int Date::getMonth() const return month; } // end function getmonth unsigned int Date::getYear() const return year; } // end function getyear void Date::print()

8 cout << month << '-' << day << '-' << year << '\n'; // outputs date } // end function print void Date::nextDay() try setday( getday() + 1 ); // increments day by 1 } catch ( invalid_argument & ) setday( 1 ); try setmonth( getmonth() + 1 ); } catch ( invalid_argument & ) setmonth( 1 ); setyear( getyear() + 1 ); } } } // end function nextday

9 bool Date::leapYear() const if ( year % 400 == 0 ( year % 4 == 0 && year % 100!= 0 ) ) return true; // is a leap year else return false; // is not a leap year } // end function leapyear int Date::monthDays() const const array< int, 12 > days = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; return month == 2 && leapyear()? 29 : days[ month - 1 ]; } // end function monthdays

10 Question % #include "TicTacToe.h" // include definition of class TicTacToe int main() TicTacToe g; // creates object g of class TicTacToe g.makemove(); // invokes function makemove } // end main // Exercise 9.15 Solution: TicTacToe.h #ifndef TICTACTOE_H #define TICTACTOE_H #include <array> class TicTacToe private: enum Status WIN, DRAW, CONTINUE }; // enumeration constants std::array< std::array< int, 3 >, 3 > board; public: TicTacToe(); // default constructor void makemove(); // make move void printboard() const; // print board

11 bool validmove( int, int ) const; // validate move bool xomove( int ); // x o move Status gamestatus() const; // game status }; // end class TicTacToe #endif // Exercise 9.15 Solution: TicTacToe.cpp // Member-function definitions for class TicTacToe. #include <iostream> #include <iomanip> #include "TicTacToe.h" // include definition of class TicTacToe using namespace std; TicTacToe::TicTacToe() for ( int j = 0; j < 3; ++j ) // initialize board for ( int k = 0; k < 3; ++k ) board[ j ][ k ] = ' '; } // end TicTacToe constructor bool TicTacToe::validMove( int r, int c ) const

12 return r >= 0 && r < 3 && c >= 0 && c < 3 && board[ r ][ c ] == ' '; } // end function validmove // must specify that type Status is part of the TicTacToe class. TicTacToe::Status TicTacToe::gameStatus() const int a; // check for a win on diagonals if ( board[ 0 ][ 0 ]!= ' ' && board[ 0 ][ 0 ] == board[ 1 ][ 1 ] && board[ 0 ][ 0 ] == board[ 2 ][ 2 ] ) return WIN; else if ( board[ 2 ][ 0 ]!= ' ' && board[ 2 ][ 0 ] == board[ 1 ][ 1 ] && board[ 2 ][ 0 ] == board[ 0 ][ 2 ] ) return WIN; // check for win in rows for ( a = 0; a < 3; ++a ) if ( board[ a ][ 0 ]!= ' ' && board[ a ][ 0 ] == board[ a ][ 1 ] && board[ a ][ 0 ] == board[ a ][ 2 ] ) return WIN; // check for win in columns

13 for ( a = 0; a < 3; ++a ) if ( board[ 0 ][ a ]!= ' ' && board[ 0 ][ a ] == board[ 1 ][ a ] && board[ 0 ][ a ] == board[ 2 ][ a ] ) return WIN; // check for a completed game for ( int r = 0; r < 3; ++r ) for ( int c = 0; c < 3; ++c ) if ( board[ r ][ c ] == ' ' ) return CONTINUE; // game is not finished return DRAW; // game is a draw } // end function gamestatus void TicTacToe::printBoard() const cout << " 0 1 2\n\n"; for ( int r = 0; r < 3; ++r ) cout << r; for ( int c = 0; c < 3; ++c )

14 cout << setw( 3 ) << static_cast< char > ( board[ r ][ c ] ); if ( c!= 2 ) cout << " "; } // end for if ( r!= 2 ) cout << "\n \n \n"; } // end for cout << "\n\n"; } // end function printboard void TicTacToe::makeMove() printboard(); while ( true ) if ( xomove( 'X' ) ) break; else if ( xomove( 'O' ) ) break; } // end while structure

15 } // end function makemove bool TicTacToe::xoMove( int symbol ) int x; int y; do cout << "Player " << static_cast< char >( symbol ) << " enter move: "; cin >> x >> y; cout << '\n'; } while (!validmove( x, y ) ); board[ x ][ y ] = symbol; printboard(); Status xostatus = gamestatus(); if ( xostatus == WIN ) cout << "Player " << static_cast< char >( symbol ) << " wins!\n"; return true; } // end if

16 else if ( xostatus == DRAW ) cout << "Game is a draw.\n"; return true; } // end else if else // CONTINUE return false; } // end function xomove

17 Question % // Exercise 9.23 Solution: ex09_23.cpp // Card shuffling and dealing program. #include <iostream> #include <iomanip> #include "DeckOfCards.h" // DeckOfCards class definition using namespace std; int main() DeckOfCards mydeckofcards; mydeckofcards.shuffle(); // place Cards in random order // print all 52 Cards in the order in which they are dealt for ( int i = 1; mydeckofcards.morecards(); ++i ) // deal and display a Card cout << left << setw( 19 ) << mydeckofcards.dealcard().tostring(); if ( i % 4 == 0 ) // output newline every 4 cards cout << endl; } // end for } // end main

18 // Exercise 9.23 Solution: Card.h // Class Card represents the face and suit of a card. #ifndef CARD_H #define CARD_H #include <string> class Card public: static const size_t FACES = 13; // total number of faces static const size_t SUITS = 4; // total number of suits Card( int cardface, int cardsuit ); // initialize face and suit std::string tostring() const; // returns a string representation of a Card // get the card's face int getface() const return face; } // end function getface // get the card's suit

19 int getsuit() const return suit; } // end function getsuit private: int face; int suit; static const std::string facenames[ FACES ]; static const std::string suitnames[ SUITS ]; }; // end class Card #endif // Exercise 9.23 Solution: Card.cpp // Member-function definitions and array initializers for the Card class. #include "Card.h" using namespace std; // contents of arrays to convert index into name const string Card::faceNames[ FACES ] = "Ace", "Deuce", "Three", "Four", "Five", "Six",

20 "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; const string Card::suitNames[ SUITS ] = "Hearts", "Diamonds", "Clubs", "Spades" }; // Card constructor initializes face and suit Card::Card( int cardface, int cardsuit ) face = ( cardface >= 0 && cardface < FACES )? cardface : 0; suit = ( cardsuit >= 0 && cardsuit < SUITS )? cardsuit : 0; } // end Card constructor // returns a string representation of a Card string Card::toString() const // strings can be concatenated with + return facenames[ face ] + " of " + suitnames[ suit ]; } // end function tostring // Exercise 9.23 Solution: DeckOfCards.h // Class DeckOfCards represents a deck of 52 playing cards. #ifndef DECK_OF_CARDS_H #define DECK_OF_CARDS_H

21 #include <vector> #include "Card.h" // DeckOfCards class definition class DeckOfCards public: DeckOfCards(); // constructor initializes deck void shuffle(); // shuffles cards in deck Card dealcard(); // deals cards in deck bool morecards() const; // are there any more cards left private: std::vector< Card > deck; // represents deck of cards unsigned currentcard; // index of next card to be dealt }; // end class DeckOfCards #endif // Exercise 9.23 Solution: DeckOfCards.cpp // Member-function definitions for class DeckOfCards that simulates // the shuffling and dealing of a deck of playing cards. #include <random> // contains C++11 random number generation features #include <ctime>

22 #include "DeckOfCards.h" // DeckOfCards class definition using namespace std; default_random_engine engine( static_cast<unsigned int>( time(0) ) ); // DeckOfCards default constructor initializes deck DeckOfCards::DeckOfCards() currentcard = 0; // set currentcard so first Card dealt is deck[ 0 ] // populate deck with Card objects for ( size_t i = 0; i < Card::FACES * Card::SUITS; ++i ) Card card( i % Card::FACES, i / Card::SUITS ); deck.push_back( card ); // adds copy of card to the end of the deck } // end for } // end DeckOfCards default constructor // shuffle cards in deck void DeckOfCards::shuffle() uniform_int_distribution<size_t> randomint( 0, deck.size() - 1 ); // after shuffling, dealing should start at deck[ 0 ] again

23 currentcard = 0; // reinitialize currentcard // for each Card, pick another random Card and swap them for ( size_t first = 0; first < deck.size(); ++first ) // select a random number between 0 and 51 unsigned second = randomint( engine ); // swap current Card with randomly selected Card Card temp = deck[ first ]; deck[ first ] = deck[ second ]; deck[ second ] = temp; } // end for } // end function shuffle // deal cards in deck Card DeckOfCards::dealCard() return deck[ currentcard++ ]; // return current Card in vector } // end function dealcard // Check if there are more cards in the deck bool DeckOfCards::moreCards() const

24 return currentcard < deck.size(); } // end function morecards

25 Question % // Exercise 9.24 Solution: ex09_24.cpp // Card shuffling and dealing program. #include <iostream> #include <iomanip> #include "DeckOfCards.h" // DeckOfCards class definition #include "Hand.h" // Hand class definition using namespace std; int main() DeckOfCards mydeckofcards; mydeckofcards.shuffle(); // place Cards in random order Hand hand( mydeckofcards ); // deal a hand from the deck hand.print(); // display hand // check for each type of hand by decreasing ranking if ( hand.fourofakind() ) cout << "Hand contains four of a kind" << endl; else if ( hand.flush() && hand.straight() ) cout << "Hand contains a straight flush" << endl; else if ( hand.flush() ) cout << "Hand contains a flush" << endl;

26 else if ( hand.straight() ) cout << "Hand contains a straight" << endl; else if ( hand.threeofakind() ) cout << "Hand contains three of a kind" << endl; else if ( hand.twopair() ) cout << "Hand contains two pairs" << endl; else if ( hand.pair() ) cout << "Hand contains a pair" << endl; } // end main // Exercise 9.24 Solution: Card.h // Class Card represents the face and suit of a card. #ifndef CARD_H #define CARD_H #include <string> class Card public: static const size_t FACES = 13; // total number of faces

27 static const size_t SUITS = 4; // total number of suits Card( int cardface, int cardsuit ); // initialize face and suit std::string tostring() const; // returns a string representation of a Card // get the card's face int getface() const return face; } // end function getface // get the card's suit int getsuit() const return suit; } // end function getsuit private: int face; int suit; static const std::string facenames[ FACES ]; static const std::string suitnames[ SUITS ]; }; // end class Card

28 #endif // Exercise 9.24 Solution: Card.cpp // Member-function definitions and array initializers for the Card class. #include "Card.h" using namespace std; // contents of arrays to convert index into name const string Card::faceNames[ FACES ] = "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; const string Card::suitNames[ SUITS ] = "Hearts", "Diamonds", "Clubs", "Spades" }; // Card constructor initializes face and suit Card::Card( int cardface, int cardsuit ) face = ( cardface >= 0 && cardface < FACES )? cardface : 0; suit = ( cardsuit >= 0 && cardsuit < SUITS )? cardsuit : 0; } // end Card constructor // returns a string representation of a Card string Card::toString() const // strings can be concatenated with +

29 return facenames[ face ] + " of " + suitnames[ suit ]; } // end function tostring // Exercise 9.24 Solution: DeckOfCards.h // Class DeckOfCards represents a deck of 52 playing cards. #ifndef DECK_OF_CARDS_H #define DECK_OF_CARDS_H #include <vector> #include "Card.h" // DeckOfCards class definition class DeckOfCards public: DeckOfCards(); // constructor initializes deck void shuffle(); // shuffles cards in deck Card dealcard(); // deals cards in deck bool morecards() const; // are there any more cards left private: std::vector< Card > deck; // represents deck of cards unsigned currentcard; // index of next card to be dealt

30 }; // end class DeckOfCards #endif // Exercise 9.24 Solution: DeckOfCards.cpp // Member-function definitions for class DeckOfCards that simulates // the shuffling and dealing of a deck of playing cards. #include <random> // contains C++11 random number generation features #include <ctime> #include "DeckOfCards.h" // DeckOfCards class definition using namespace std; default_random_engine engine( static_cast<unsigned int>( time(0) ) ); // DeckOfCards default constructor initializes deck DeckOfCards::DeckOfCards() currentcard = 0; // set currentcard so first Card dealt is deck[ 0 ] // populate deck with Card objects for ( size_t i = 0; i < Card::FACES * Card::SUITS; ++i )

31 Card card( i % Card::FACES, i / Card::SUITS ); deck.push_back( card ); // adds copy of card to the end of the deck } // end for } // end DeckOfCards default constructor // shuffle cards in deck void DeckOfCards::shuffle() uniform_int_distribution<size_t> randomint( 0, deck.size() - 1 ); // after shuffling, dealing should start at deck[ 0 ] again currentcard = 0; // reinitialize currentcard // for each Card, pick another random Card and swap them for ( size_t first = 0; first < deck.size(); ++first ) // select a random number between 0 and 51 unsigned second = randomint( engine ); // swap current Card with randomly selected Card Card temp = deck[ first ]; deck[ first ] = deck[ second ]; deck[ second ] = temp; } // end for

32 } // end function shuffle // deal cards in deck Card DeckOfCards::dealCard() return deck[ currentcard++ ]; // return current Card in vector } // end function dealcard // Check if there are more cards in the deck bool DeckOfCards::moreCards() const return currentcard < deck.size(); } // end function morecards // Exercise 9.24 Solution: Hand.h // Stores and calculates attributes of a hand of cards. #ifndef HAND_H #define HAND_H #include <string> #include <vector> #include "Card.h"

33 #include "DeckOfCards.h" class Hand public: // constructor takes 5 cards from Deck Hand( DeckOfCards &deck ); void print() const; // display hand // determine if we have the given scoring hand bool pair() const; bool twopair() const; bool threeofakind() const; bool fourofakind() const; bool flush() const; bool straight() const; private: std::vector< Card > hand; // our hand std::vector< int > facecount; // number of each face }; // end class Hand #endif

34 // Exercise 9.24 Solution: Hand.cpp // Stores and calculates attributes of a hand of cards. #include <iostream> #include "Hand.h" using namespace std; // constructor takes 5 cards from Deck; it assumes 5 cards are available Hand::Hand( DeckOfCards &deck ) : facecount( Card::FACES ) // get hand from deck for ( unsigned i = 0; i < 5; ++i ) hand.push_back( deck.dealcard() ); // count number of times each face appears (for later use) for ( unsigned i = 0; i < hand.size(); ++i ) ++facecount[ hand[ i ].getface() ]; } // end constructor // display contents of hand to standard output stream void Hand::print() const cout << "Hand is:\n";

35 for ( unsigned i = 0; i < hand.size(); ++i ) cout << hand[ i ].tostring() << '\n'; cout << endl; } // end function print // determine if hand contains a pair bool Hand::pair() const for ( unsigned i = 0; i < facecount.size(); ++i ) if ( facecount[ i ] == 2 ) return true; return false; } // end function pair // determine if hand contains two pairs bool Hand::twoPair() const bool foundone = false; // true if the first pair has been found for ( unsigned i = 0; i < facecount.size(); ++i ) if ( facecount[ i ] == 2 && foundone ) return true; // found both pairs

36 else if ( facecount[ i ] == 2 ) foundone = true; // found the first pair } // end for return false; // didn't find both pairs } // end function twopair // determine if hand contains three of a kind bool Hand::threeOfAKind() const for ( unsigned i = 0; i < facecount.size(); ++i ) if ( facecount[ i ] == 3 ) return true; return false; } // end function threeofakind // determine if hand contains four of a kind bool Hand::fourOfAKind() const for ( unsigned i = 0; i < facecount.size(); ++i ) if ( facecount[ i ] == 4 ) return true;

37 return false; } // end function fourofakind // determine if hand contains a flush bool Hand::flush() const int suit = hand[ 0 ].getsuit(); // search for any card that has a suit different from the first for ( unsigned i = 1; i < hand.size(); ++i ) if ( hand[ i ].getsuit()!= suit ) return false; return true; } // end function flush // determine if hand contains a straight bool Hand::straight() const vector< int > tmp = facecount; // make a temporary copy of face count tmp.push_back( tmp[ 0 ] ); // ace can also be used as the high card // special case: ace straight if ( tmp[ 0 ] == 1 && tmp[ 1 ] == 1 && tmp[ 2 ] == 1 &&

38 tmp[ 3 ] == 1 && tmp[ 4 ] == 1 ) return true; unsigned i = 1; // counter to hold current position // search for first non-zero face count while ( i < tmp.size() && tmp[ i ] == 0 ) ++i; unsigned start = i; // save position // count consecutive frequencies of 1 while ( i < tmp.size() && tmp[ i ] == 1 ) ++i; return i == start + 5; // should have counted 5 faces with frequency 1 } // end function straight

39

//main.cpp. using namespace std;

//main.cpp. using namespace std; Eric Villanueva CSE 330 Homework 1 (40/40) I d give myself a full 40 out of 40 points. I put a lot of effort into getting my code to work, I also used my own code for most of it. Question 1) #include

More information

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Pointers and Strings Prentice Hall, Inc. All rights reserved. Pointers and Strings 1 sizeof operator Pointer Expressions and Pointer Arithmetic Relationship Between Pointers and Arrays Arrays of Pointers Case Study: Card Shuffling and Dealing Simulation sizeof operator

More information

Page 1 of 5. *** person.cpp *** #include "person.h" #include <string> #include <iostream> using namespace std; *** person.h ***

Page 1 of 5. *** person.cpp *** #include person.h #include <string> #include <iostream> using namespace std; *** person.h *** #ifndef PERSON_H #define PERSON_H #include "date.h" *** person.h *** class Person // constructors Person( ); Person( const string &name, const int id ); Person( const string &name, const int id, const

More information

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 1 9 Classes: A Deeper Look, Part 1 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition errors caused by including more than one copy of a header file

More information

Classes: A Deeper Look, Part 2

Classes: A Deeper Look, Part 2 10 But what, to serve our private ends, Forbids the cheating of our friends? Charles Churchill Instead of this absurd division into sexes they ought to class people as static and dynamic. Evelyn Waugh

More information

Introduction. W8.2 Operator Overloading

Introduction. W8.2 Operator Overloading W8.2 Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. as friend Functions Overloading Stream Insertion and Extraction

More information

Operator Overloading

Operator Overloading Operator Overloading Introduction Operator overloading Enabling C++ s operators to work with class objects Using traditional operators with user-defined objects Requires great care; when overloading is

More information

Review Questions II KEY

Review Questions II KEY CS 102 / ECE 206 Spring 2011 Review Questions II KEY The following review questions are similar to the kinds of questions you will be expected to answer on Exam II (April 7), which will focus on LCR, chs.

More information

Chapter 6 - Pointers

Chapter 6 - Pointers Chapter 6 - Pointers Outline 1 Introduction 2 Pointer Variable Declarations and Initialization 3 Pointer Operators 4 Calling Functions by Reference 5 Using the const Qualifier with Pointers 6 Bubble Sort

More information

W8.2 Operator Overloading

W8.2 Operator Overloading 1 W8.2 Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. as friend Functions Overloading Stream Insertion and Extraction

More information

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then 1 7 C Pointers 7.7 sizeof Operator 2 sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then int myarray[ 10 ]; printf( "%d", sizeof(

More information

CIS 190: C/C++ Programming. Classes in C++

CIS 190: C/C++ Programming. Classes in C++ CIS 190: C/C++ Programming Classes in C++ Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors Headers in C++ done same way as in C including user.h files:

More information

Operator Overloading

Operator Overloading Operator Overloading Introduction Operator overloading Enabling C++ s operators to work with class objects Using traditional operators with user-defined objects Requires great care; when overloading is

More information

第三章习题答案 // include definition of class GradeBook from GradeBook.h #include "GradeBook.h"

第三章习题答案 // include definition of class GradeBook from GradeBook.h #include GradeBook.h 第三章习题答案 3.11 // Exercise 3.11 Solution: GradeBook.h // Definition of GradeBook class that stores an instructor's name. #include // program uses C++ standard string class using std::string; //

More information

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings Chapter 5 - Pointers and Strings 1 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization 5.3 Pointer Operators 5.4 Calling Functions by Reference 5.5 Using const with Pointers 5.6 Bubble

More information

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings Chapter 5 - Pointers and Strings 1 5.1 Introduction 2 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization 5.3 Pointer Operators 5. Calling Functions by Reference 5.5 Using const with

More information

CSC 533: Organization of Programming Languages. Spring 2007

CSC 533: Organization of Programming Languages. Spring 2007 CSC 533: Organization of Programming Languages Spring 2007 Java vs. C++ C++ design goals C++ reliability features by-reference, const, new & delete, bool, string C++ OOP features classes, bindings, templates,

More information

1 of 8 3/28/2010 8:03 AM C++ Special Topics Home Class Info Links Lectures Newsgroup Assignmen This is a short review of special topics in C++ especially helpful for various assignments. These notes are

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Designing an ADT Kostas Alexis Abstract Data Type Abstract Data Type (ADT) A specification for a group of values and operations on those values Defined (conceptually)

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. Data structures Collections of related data items. Discussed in depth in Chapters 16 21. Array objects Data

More information

Operator overloading: extra examples

Operator overloading: extra examples Operator overloading: extra examples CS319: Scientific Computing (with C++) Niall Madden Week 8: some extra examples, to supplement what was covered in class 1 Eg 1: Points in the (x, y)-plane Overloading

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Pointers Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 7.1 Introduction 7.2 Pointer Variable Definitions and Initialization 7.3 Pointer

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game

More information

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana POINTERS Programación de Computadores Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana 2018-01 Pointers A pointer is a reference to

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1 Introduction 10.2 Structure Definitions 10.3 Initializing Structures 10.4 Accessing

More information

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures Self-referencing Card Shuffling Unions CS 2060 Self-referencing Card Shuffling Unions : struct C enables us to aggregate diverse data types into a structure: struct Employee { char first_name[20]; char

More information

The goal of this project is create a program to have a human user play against the computer.

The goal of this project is create a program to have a human user play against the computer. CS 7B - Fall 217 - Assignment 4: The Game of Set. Due 12/4/17 The Game of Set Rules (this is a hyperlink to https://www.setgame.com/set/puzzle rules) should be studied carefully if you are still unsure

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14. Lecture 14 1 Exceptions Iterators Random numbers Casting Enumerations Pairs The Big Three Outline 2 Error Handling It is often easier to write a program by first assuming that nothing incorrect will happen

More information

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... CISC 2000 Computer Science II Fall, 2014 Note 12/1/2014 1 Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... (a) What s the purpose of inheritance?

More information

Relationship between Pointers and Arrays

Relationship between Pointers and Arrays Relationship between Pointers and Arrays Arrays and pointers are intimately related in C and often may be used interchangeably. An array name can be thought of as a constant pointer. Pointers can be used

More information

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Lecture 3 Winter 2011/12 Oct 25 Visual C++: Problems and Solutions New section on web page (scroll down)

More information

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1 DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1 March 11, 2018 Question 1: Identify the common elements of two sorted

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/45/lab45.(C CPP cpp c++ cc cxx cp) Input: under control of main function Output: under control of main function Value: 4 Integer data is usually represented in a single word on a computer.

More information

Lecture 05 POINTERS 1

Lecture 05 POINTERS 1 Lecture 05 POINTERS 1 Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings Pointer Variable vs. Normal Variable Normal variables contain a specific

More information

Object oriented programming

Object oriented programming Exercises 12 Version 1.0, 9 May, 2017 Table of Contents 1. Virtual destructor and example problems...................................... 1 1.1. Virtual destructor.......................................................

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Programming for Engineers Pointers

Programming for Engineers Pointers Programming for Engineers Pointers ICEN 200 Spring 2018 Prof. Dola Saha 1 Pointers Pointers are variables whose values are memory addresses. A variable name directly references a value, and a pointer indirectly

More information

Fundamentals of Programming Session 25

Fundamentals of Programming Session 25 Fundamentals of Programming Session 25 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

A Deeper Look at Classes

A Deeper Look at Classes A Deeper Look at Classes Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by

More information

Classes: A Deeper Look. Systems Programming

Classes: A Deeper Look. Systems Programming Classes: A Deeper Look Systems Programming Deeper into C++ Classes const objects and const member functions Composition Friendship this pointer Dynamic memory management new and delete operators static

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Multiple Inheritance Template Classes and Functions Inheritance is supported by most object oriented languages C++ allows a special kind of inheritance known

More information

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

Classes Classes 2 / 35

Classes Classes 2 / 35 Classes 1 / 35 Classes Classes 2 / 35 Anatomy of a Class By the end of next lecture, you ll understand everything in this class definition. package edu. gatech. cs1331. card ; import java. util. Arrays

More information

Linked List using a Sentinel

Linked List using a Sentinel Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

2. It is possible for a structure variable to be a member of another structure variable.

2. It is possible for a structure variable to be a member of another structure variable. FORM 1(put name, form, and section number on scantron!!!) CS 162 Exam I True (A) / False (B) (2 pts) 1. What value will the function eof return if there are more characters to be read in the input stream?

More information

Chapter 18 - C++ Operator Overloading

Chapter 18 - C++ Operator Overloading Chapter 18 - C++ Operator Overloading Outline 18.1 Introduction 18.2 Fundamentals of Operator Overloading 18.3 Restrictions on Operator Overloading 18.4 Operator Functions as Class Members vs. as friend

More information

7. Arrays, More Java Looping

7. Arrays, More Java Looping 7-1 7. Arrays, More Java Looping Review and Preview In the last class, we introduced the idea of looping repeating code blocks. In this class Java lesson, we look at another way to loop (the Java for loop)

More information

Chapter 9 Classes : A Deeper Look, Part 1

Chapter 9 Classes : A Deeper Look, Part 1 Chapter 9 Classes : A Deeper Look, Part 1 C++, How to Program Deitel & Deitel Fall 2016 CISC1600 Yanjun Li 1 Time Class Case Study Time Class Definition: private data members: int hour; int minute; int

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

More information

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions Introduction Structures, Unions, Bit Manipulations, and Enumerations In C, we can create our own data types If programmers do a good job of this, the end user does not even have to know what is in the

More information

Operator Overloading in C++ Systems Programming

Operator Overloading in C++ Systems Programming Operator Overloading in C++ Systems Programming Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. Global Functions Overloading

More information

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011 The American University in Cairo Department of Computer Science & Engineering CSCI 106-07&09 Dr. KHALIL Exam-I Fall 2011 Last Name :... ID:... First Name:... Form I Section No.: EXAMINATION INSTRUCTIONS

More information

C Pointers Pearson Education, Inc. All rights reserved.

C Pointers Pearson Education, Inc. All rights reserved. 1 7 C Pointers 2 Addresses are given to us to conceal our whereabouts. Saki (H. H. Munro) By indirection find direction out. William Shakespeare Many things, having full reference To one consent, may work

More information

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as Pointers and Strings Chapters 10, 12 2/5/07 CS250 Introduction to Computer Science II 1 3.1 Pointers and Arrays (10.3) An array of ints can be declared as o int numbers[] = 1, 2, 3, 4, 5; numbers is also

More information

Computer Programming with C++ (21)

Computer Programming with C++ (21) Computer Programming with C++ (21) Zhang, Xinyu Department of Computer Science and Engineering, Ewha Womans University, Seoul, Korea zhangxy@ewha.ac.kr Classes (III) Chapter 9.7 Chapter 10 Outline Destructors

More information

Programming Assignment #2

Programming Assignment #2 Programming Assignment #2 Due: 11:59pm, Wednesday, Feb. 13th Objective: This assignment will provide further practice with classes and objects, and deepen the understanding of basic OO programming. Task:

More information

Assignment 2 Solution

Assignment 2 Solution Assignment 2 Solution Date.h #ifndef DATE_H #define DATE_H #include class Date time_t date; public: Date(); Date(const Date&) = default; Date(time_t); // Date in time_t format Date(const char*);

More information

Templates and Vectors

Templates and Vectors Templates and Vectors 1 Generic Programming function templates class templates 2 the STL vector class a vector of strings enumerating elements with an iterator inserting and erasing 3 Writing our own vector

More information

Section Handout #4: Classes, Pointers, and Dynamic Memory

Section Handout #4: Classes, Pointers, and Dynamic Memory Nick Troccoli Section #4 CS 106X Week 5 Section Handout #4: Classes, Pointers, and Dynamic Memory Based on handouts by various current and past CS106B/X instructors and TAs. Extra practice problems: CodeStepByStep

More information

Input And Output of C++

Input And Output of C++ Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

Implementing an ADT with a Class

Implementing an ADT with a Class Implementing an ADT with a Class the header file contains the class definition the source code file normally contains the class s method definitions when using Visual C++ 2012, the source code and the

More information

Week 3: Pointers (Part 2)

Week 3: Pointers (Part 2) Advanced Programming (BETC 1353) Week 3: Pointers (Part 2) Dr. Abdul Kadir abdulkadir@utem.edu.my Learning Outcomes: Able to describe the concept of pointer expression and pointer arithmetic Able to explain

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2305/lab06.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 2 Extend the IntegerSet class from Lab 04 to provide the following

More information

Fig. 7.1 Fig. 7.2 Fig. 7.3 Fig. 7.4 Fig. 7.5 Fig. 7.6 Fig. 7.7 Fig. 7.8 Fig. 7.9 Fig. 7.10

Fig. 7.1 Fig. 7.2 Fig. 7.3 Fig. 7.4 Fig. 7.5 Fig. 7.6 Fig. 7.7 Fig. 7.8 Fig. 7.9 Fig. 7.10 CHAPTER 7 CLASSES: PART II 1 Illustrations List (Main Page) Fig. 7.1 Fig. 7.2 Fig. 7.3 Fig. 7.4 Fig. 7.5 Fig. 7.6 Fig. 7.7 Fig. 7.8 Fig. 7.9 Fig. 7.10 Using a Time class with const objects and const member

More information

การทดลองท 8_2 Editor Buffer Array Implementation

การทดลองท 8_2 Editor Buffer Array Implementation การทดลองท 8_2 Editor Buffer Array Implementation * File: buffer.h * -------------- * This file defines the interface for the EditorBuffer class. #ifndef _buffer_h #define _buffer_h * Class: EditorBuffer

More information

C++ Programming Assignment 3

C++ Programming Assignment 3 C++ Programming Assignment 3 Author: Ruimin Zhao Module: EEE 102 Lecturer: Date: Fei.Xue April/19/2015 Contents Contents ii 1 Question 1 1 1.1 Specification.................................... 1 1.2 Analysis......................................

More information

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O Outline EDAF30 Programming in C++ 2. Introduction. More on function calls and types. Sven Gestegård Robertz Computer Science, LTH 2018 1 Function calls and parameter passing 2 Pointers, arrays, and references

More information

Deitel Series Page How To Program Series

Deitel Series Page How To Program Series Deitel Series Page How To Program Series Android How to Program C How to Program, 7/E C++ How to Program, 9/E C++ How to Program, Late Objects Version, 7/E Java How to Program, 9/E Java How to Program,

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/11/lab11.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 1 The purpose of this assignment is to become more familiar with

More information

Computer Programming Class Members 9 th Lecture

Computer Programming Class Members 9 th Lecture Computer Programming Class Members 9 th Lecture 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University Copyrights 2015 Eom, Hyeonsang All Rights Reserved Outline Class

More information

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1 Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I Review from Lecture 2 CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I Vectors are dynamically-sized arrays Vectors, strings and other containers should be: passed by reference when they are to

More information

Topics. bool and string types input/output library functions comments memory allocation templates classes

Topics. bool and string types input/output library functions comments memory allocation templates classes C++ Primer C++ is a major extension of c. It is similar to Java. The lectures in this course use pseudo-code (not C++). The textbook contains C++. The labs involve C++ programming. This lecture covers

More information

Time(int h, int m, int s) { hrs = h; mins = m; secs = s; } void set(int h, int m, int s) { hrs = h; mins = m; secs = s; }

Time(int h, int m, int s) { hrs = h; mins = m; secs = s; } void set(int h, int m, int s) { hrs = h; mins = m; secs = s; } Inheritance The following class implements time in hh:mm::ss format: class { public: () { = 0; = 0; = 0; (int h, int m, int s) { = h; = m; = s; void set(int h, int m, int s) { = h; = m; = s; void tick()

More information

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++ COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.

More information

Midterm Review. PIC 10B Spring 2018

Midterm Review. PIC 10B Spring 2018 Midterm Review PIC 10B Spring 2018 Q1 What is size t and when should it be used? A1 size t is an unsigned integer type used for indexing containers and holding the size of a container. It is guarenteed

More information

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

Sample file. Practice Exam One COMPUTER SCIENCE A SECTION I. Directions: Determine the answer to each of the following questions or incomplete

Sample file. Practice Exam One COMPUTER SCIENCE A SECTION I. Directions: Determine the answer to each of the following questions or incomplete Practice Exam One / Level A Diagnostic Test 5 Practice Exam One COMPUTER SCIENCE A SECTION I Time 1 hour and 15 minutes Number of questions 40 Percent of total grade 50 Directions: Determine the answer

More information

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers. cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... today: language basics: identifiers, data types, operators, type conversions, branching and looping, program structure

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes... CISC 2000 Computer Science II Fall, 2014 Note 11/13/2014 1 Review of operator overloading (a) Lab class: take-away ############################ # Pass-by-value parameters # ############################

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism 1 Inheritance extending a clock to an alarm clock deriving a class 2 Polymorphism virtual functions and polymorphism abstract classes MCS 360 Lecture 8 Introduction to Data

More information

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture: CISC 2200 Data Structure Fall, 2016 C++ Review:3/3 1 From last lecture: pointer type and pointer variable (stores memory addresses of a variable (of any type, local or global, automatic/static/dynamic)

More information

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations Outline 12.1 Test-Driving the Craps Game Application 12.2 Random Number Generation 12.3 Using an enum in the Craps

More information

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 5 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 13, 2013 Question 1 Consider the following Java definition of a mutable string class. class MutableString private char[] chars

More information

Lecture 23: Pointer Arithmetic

Lecture 23: Pointer Arithmetic Lecture 23: Pointer Arithmetic Wai L. Khoo Department of Computer Science City College of New York November 29, 2011 Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 1 / 14 Pointer Arithmetic Pointer

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

Hashing. inserting and locating strings. MCS 360 Lecture 28 Introduction to Data Structures Jan Verschelde, 27 October 2010.

Hashing. inserting and locating strings. MCS 360 Lecture 28 Introduction to Data Structures Jan Verschelde, 27 October 2010. ing 1 2 3 MCS 360 Lecture 28 Introduction to Data Structures Jan Verschelde, 27 October 2010 ing 1 2 3 We covered STL sets and maps for a frequency table. The STL implementation uses a balanced search

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number Generation

More information

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

Programming 2. Object Oriented Programming. Daniel POP

Programming 2. Object Oriented Programming. Daniel POP Programming 2 Object Oriented Programming Daniel POP Week 5 Agenda 1. Modifiers: friend 2. Objects Wrap-up last week Self-reference Modifiers: static const mutable Object Oriented Programming Friends (I)

More information