Use the template below and fill in the areas in Red to complete it.

Similar documents
CSCI 102 Fall 2010 Exam #1

Inheritance, and Polymorphism.

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

Use the dot operator to access a member of a specific object.

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

True or False (12 Points)

Inheritance and Overloading. Week 11

Partha Sarathi Mandal

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

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Name Section: M/W or T/TH. True or False (14 Points)

Constructor - example

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

OBJECT ORIENTED PROGRAMMING

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

Homework 11 Program Setup (with some IMPORTANT NEW STEPS!)

Programming in C++: Assignment Week 5

CMSC 202 Midterm Exam 1 Fall 2015

Object-Oriented Programming Concepts

Name SECTION: 12:45 2:20. True or False (12 Points)

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Local and Global Variables

CSC1322 Object-Oriented Programming Concepts

Data Structures (INE2011)

15: Polymorphism & Virtual Functions

pointers + memory double x; string a; int x; main overhead int y; main overhead

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Object Oriented Programming: Inheritance Polymorphism

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Implementing an ADT with a Class

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CS 376b Computer Vision

C++ (classes) Hwansoo Han

COEN244: Class & function templates

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Spring CS Homework 12 p. 1. CS Homework 12

Polymorphism CSCI 201 Principles of Software Development

a. a * c - 10 = b. a % b + (a * d) + 7 =

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Computer Programming : C++

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

Homework Assignment #2 (revised)

CSCE Practice Midterm. Data Types

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Class Sale. Represents sales of single item with no added discounts or charges. Notice reserved word "virtual" in declaration of member function bill

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Fast Introduction to Object Oriented Programming and C++

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

More C++ : Vectors, Classes, Inheritance, Templates

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

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

Namespaces and Class Hierarchies

EECS402 Lecture 02. Functions. Function Prototype

Programming. C++ Basics

OBJECT ORIENTED PROGRAMMING USING C++

CS 162, Lecture 25: Exam II Review. 30 May 2018

Chapter 1: Object-Oriented Programming Using C++

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

CSCE 110 PROGRAMMING FUNDAMENTALS

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Object oriented programming

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Ch. 17: Linked Lists. Introduction to Linked Lists

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

EP241 Computer Programming

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Lab 12 Object Oriented Programming Dr. John Abraham

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

CS18000: Problem Solving And Object-Oriented Programming

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

CSCI 104 Polymorphism. Mark Redekopp David Kempe

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Introduction to Programming using C++

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

CS24 Week 3 Lecture 1

Object Oriented Design

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

CS 115 Exam 3, Spring 2010

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

CS 11 C++ track: lecture 1

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Part I: Short Answer (12 questions, 65 points total)

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

CSCI 101L - Data Structures. Practice problems for Final Exam. Instructor: Prof Tejada

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Transcription:

C++ with Inheritance Pproblem involving inheritance. You have to finish completing code that creates a class called shape, from which 3 classes are derived that are called square and triangle. I am giving you the code for the shape class and the square class. Your job is to add code for a triangle class and modify the statements that call and print out the values at the end of the program. A TEMPLATE file has been provided that provides much of the code. The code that needs to be added is highlighted in red. HINTS file provide a good plan on how to do the triangle code. The following is the data for the objects: Shape id = 100 Square id = 200, side length = 4 Triangle id = 300, sides are 1, 2, and 4 As a challenge, add the code for the Circle code... a few hints are provided in the previously mentioned HINTS file. Circle id = 400, radius = 1 Template to use for Assignment 4 Use the template below and fill in the areas in Red to complete it. /////////////////////////////////////////////////////////////////////// // Homework: // // Homework description: This homework will demonstrate how we // // can use inheritance to extend on a simple class // // We will define a class called shape that will support holding // // a shape ID (an integer) and a virtual public function that can // // be called to print out it's ID and perimeter (sum of its sides) // // // // From the shape, we will derive classes that represent // // different kinds of shapes. Each of these classes can print out // // its perimeter. // // // // The test main will make objects of both shape and the derived // // types and will print out the id and perimeter of each object // // // // The class shape is given, together with a shape called a square // // The assignment is to add two more shapes, called a triangle // // and a circle, modify the test main to create objects of these // // types and print out the information for all of the shapes in // // the array of shapes. // ///////////////////////////////////////////////////////////////////////

#include <iostream> using namespace std; // class shape declaration follows. It contains a private integer // // to hold the shape ID, a constructor that receives the ID and saves// // it, a public function that returns the ID, and a virtual function // // called show_data that will print out the ID and the perimeter of // // the shape. class shape private: int id; // the shape id number // public: shape (int theid); // the constructor must get an id // int get_id(); // a function to retrieve id // virtual void show_data(); // a function that can be overridden in derived classes // ; // The shape class member definitions follow: shape::shape (int theid) : id(theid) // nothing to do since id has been saved // int shape::get_id() // get_id returns the id for the object // return id; // return the id number // void shape::show_data() // show_data will retrieve the id and print a msg // int ID = get_id(); // get the id value // cout << "object " << ID << " is a shape that has no perimeter!" << endl; //////////////////////////// this ends class shape ///////////////////// //////////////////// derived class square follows ////////////////////// // A square has 4 sides of equal length, so we need to only have // // a single private data item holding the side length. // // It will need a square constructor will has two parameters // // - the id of the square. It will pass the id during // // construction back to the shape part of the class // // - the length of a square side (a float) // // It will need a public member function called perimeter that will // // compute the perimeter of a square and return it to the caller // // It will need a show_data public member function to print out the // // square id and a computed perimeter of the square // //////////////////////////////////////////////////////////////////////// class square : public shape private: float sidelength; // the length of a side // public: square (int theid, float thesidelength); // the square constructor //

float perimeter(); // computes and returns the perimeter of a square // void show_data(); // prints the id and perimeter // ; ///// the square class member functions follow: // square::square(int theid, float thesidelength) : sidelength(thesidelength), shape(theid) // verything initialized, done // float square::perimeter() // the perimeter of a square is 4 * a side length return 4 * sidelength; // return the perimeter // void square::show_data() // retrieve the square ID and printout the id and perimeter // since we inherited the public member functions of shape, // we can call get_id to get the shape it for the square cout << "object " << get_id() << " is a square with a side of " << sidelength << " and a perimeter of " << perimeter() << endl; ///////////// end of derived class square /////////// // HINT: the perimeter of a triangle is the sum of its sides // // the perimeter of a circle is 2 * 3.14159 * radius // //////////////////// derived class triangle follows //////////////////// // A triangle has 3 sides, so we need to save the 3 side lengths // // It will need a triangle constructor will has 4 parameters // // - the id of the triangle. It will pass the id during // // construction back to the shape part of the class // // - the 3 sides of the triangle (use floats) // // It will need a public member function called perimeter that will // // compute the perimeter of a triangle and return it to the caller // // It will need a show_data public member function to print out the // // triangle id and a computed perimeter of the triangle // //////////////////////////////////////////////////////////////////////// //ADD CODE FOR THE TRIANGLE CLASS HERE ///// the triangle class member functions follow: // ///////////// end of derived class triangle /////////// //ADD CODE FOR THE CIRCLE CLASS HERE //////////////////// derived class circle follows ////////////////////// // A circle has a radius, so we need to only have // // a single private data item holding the radius. // // It will need a circle constructor will has two parameters //

// - the id of the circle. It will pass the id during // // construction back to the shape part of the class // // - the radius (use a float) // // It will need a public member function called perimeter that will // // compute the perimeter of a circle and return it to the caller // // It will need a show_data public member function to print out the // // circle id and a computed perimeter of the circle // //////////////////////////////////////////////////////////////////////// ///////////// end of derived class circle /////////// // The test main follows, performing the following steps: // 1. It makes a shape object // 2. It makes a square object // 3. It makes a triangle object // 4. It makes a circle object // 5. It makes an array of pointer to shape objects, placing the 4 objects in the array // 6. It loops through the array, printing out the information int main() //UPDATE THE NUMBER OF SHAPES WHEN YOU ADD THE CIRCLE AND TRIANGLE OBJECTS #define NUMSHAPES 2 // step 1. Make a shape object with id of 100 shape ashape (100); // step 2. Make a square object with id 200 and a side of 5 square asquare (200, 5); // step 3. Make a triangle object with id 300 and sides 1,2 and 4 //ADD CODE TO MAKE A TRIANGLE HERE // step 4. Make a circle object with radius 10 and id 400 //ADD CODE TO MAKE A CIRCLE HERE // step 5. Make an array to hold pointers to the objects, placing the // placing the object pointers in the array //ADD CODE TO ADD THE TRAINGLE AND CIRCLE OBJECT POINTERS HERE shape *shapearray[numshapes] = &ashape, &asquare; // step 6. Loop through the array, printing out the information for (int loop = 0; loop < NUMSHAPES; loop++) shapearray[loop]->show_data(); // all done, return return 0;

Hints Remember that a triangle consists of three sides, and each could be different sizes, so you'll need three private variables names to represent each side. This is done in the private area below for the triangle class. The best way to do this is to review how the Square was done since that code already exists. To help you out more, here is a template for the triangle code. The items in red indicate what you need to code. You don't have to use the code below, but feel free to use it if it helps. Triangle Code 1) UPDATE THE CLASS and its CODE class triangle : public shape private: float ALength; // the length of the first side // float BLength; // the length of the second side // float CLength; // the length of the third side // public: ; // add code to define the triangle constructor // // define a variable to store the perimeter // // define show data to print the id and perimeter // ///// the triangle class member functions follow: // triangle::triangle(int theid, float SideALength, float SideBLength, float SideCLength) : CLength(SideCLength), BLength(SideBLength), ALength(SideALength), shape(theid) // everything initialized, done // float triangle::perimeter() // the perimeter of a triangle is the sum of it's sides // use the private variables in the triangle class (ALenghth, BLenghth, and CLength) // to add the three sides together and return the value. You'll need a return statement here.

void triangle::show_data() // retrieve the triangle ID and printout the id and perimeter // since we inherited the public member functions of shape, // we can call get_id to get the shape it for the square // use cout to print the object, the three sides, and the parameter... just like we did with Square 2) Update the calls to make the Triangle code work // Update the constant to 3 now... since we added a triangle object #define NUMSHAPES 3 // step 3. Make a triangle object with id 300 and sides 1,2 and 3 triangle atriangle (300, 1, 2, 3); // step 4. Make a circle object with radius 10 and id 400 // Just leave this blank for now... until we add the circle code later // step 5. Make an array to hold pointers to the objects, placing the // placing the object pointers in the array shape *shapearray[numshapes] = &ashape, &asquare, &atriangle; The Circle Code Once you get the triangle code working, then add the code for the Circle object. Use what you learned with the triangle. Then update the code at the end to call it since we now have to process a new Circle object. //UPDATE THE NUMBER OF SHAPES to 4 when you add the circle object #define NUMSHAPES 4 // step 4. Make a circle object with radius 10 and id 400 circle acircle (400, 10); // step 5. Add the Circle to the shapearray shape *shapearray[numshapes] = &ashape, &asquare, &atriangle, &acircle;