Question

1) (5 points each) For the example code on the following pages identify each of the following by clearly labeling or marking

/*     FILE: ./shapes7/shape.h     */
#include <iostream>
using std::ostream;
class shape{
    int x,y;
  public:
    shape( )
    { x=y=0;}
    shape(int xvalue, int yvalue);
    void setShape(int new_x, int new_y);
    void setX(int new_x);
    void setY(int new_y);
    int getX( ) const;
    int getY( ) const;
    virtual void move(int x, int y) = 0;
    virtual void shift(int dx, int dy) = 0;
    virtual void draw( ) = 0;
    virtual void rotate(double r) = 0;
    virtual void print(ostream&)const;
    friend ostream & operator<<(ostream & os, const shape& s);
};

#endif

/*     FILE: ./shapes7/point.h     */
#include <iostream>
#include "shape.h"
class point: public shape{

public:
point( ):shape( )
{}
point(int xvalue, int yvalue);

    void setPoint(int new_x, int new_y);
    void move(int x, int y);
    void shift(int dx, int dy);
    void rotate(double r);
    void draw( );

};

2

/*     FILE: ./shapes7/triangle.h     */
#include <iostream>
#include "point.h"
using std::ostream;
class triangle{
    point v1;
    point v2;
    point v3;
  public:
    triangle( )
    {}
    triangle(point p1, point p2, point p3);
    triangle(int px1, int py1, int px2, int py2, int px3, int py3);
    void setTriangle( int px1, int py1, int px2, int py2, int px3, int
py3);
    point getVertex1( ) const;
    point getVertex2( ) const;
    point getVertex3( ) const;
    void setVertex1(point p);
    void setVertex2(point p);
    void setVertex3(point p);
    void draw( );
    void print(ostream & os) const;
    friend ostream & operator<<(ostream & os, const triangle& t);

};

/*

FILE: ./shapes7/circle.h     */
#include <iostream>
#include <cmath>
#include "point.h"
using std::ostream;
class circle:public point{
    double radius;
  public:
    circle( ) {}
    circle(int x, int y, double radius);
    circle(point center, double radius);
    void setCenter(int x, int y);
    void setCenter(point center);
    point getCenter( ) const;
    void scale(double factor);
    void setRadius(double new_r);
    double getRadius( ) const;

void draw( );

    void print(ostream & os) const;
    friend ostream & operator<<(ostream & os, const circle& c);
};
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello dear,

Here I am attaching the images by identifying each of the given parts of code by highlighting them one by one. I also mentioned a simple explanation for each one. Hope this will help you.

1. A Class definition

A class in C++ is a user-defined type or data structure declared with keyword class that has attributes and methods (also called member variables and member functions) as its members whose access is governed by the three access specifiers private, protected or public. By defalut it is private in c++. The selected part in this picture is a class definition.

8 1 /* FILE: ./shapes7/shape.h 2 #include <iostream> 3 using std::ostream; 4. class shape 5 int x,y; 6 public: 7 shape() { x=

2. Attributes of Class triangle

Attributes are basically variables that belongs to the class. These are often referred to as "class data/ member variables". The selected part in this picture contains attributes of class triangle.

8 9 1 /* FILE: ./shapes7/triangle.h */ 2 #include <iostream> 3 #include point.h 4 using std::ostream; 5. class triangle{ 6

3. Methods of Class triangle

Methods are basically functions that belongs to the class. These are often referred to as "class functions/ member functions". The selected part in this picture contains methods of class triangle.

1 /* FILE: -/ shapes7/triangle.h 2 #include <iostream> 3 #include point.h 4 using std::ostream; 5. class triangle{ 6 point

4. All of the constructors of one of the classes.

A constructor in C++ is a special method that is automatically called when an object of a class is created. To create a constructor, we use the same name as the class, followed by parentheses (). The selected area in the following image shows all the constructors of class triangle.

1 /* FILE: ./shapes7/triangle.h 2 #include <iostream> 3 #include point.h 4 using std::ostream; 5. class triangle{ 6 point v

5. An example of function Overloading.

With function overloading, multiple functions can have the same name with different parameters. The selected area in tha following image shows an example of function overloading of setCenter function in class circle.

1- /* 2 3 FILE: ./ shapes7/circle.h 4 #include <iostream> 5 #include <cmath> 6 #include point.h 7 using std::ostream; 8. cl

6. A class defined using Composition

Composition is generally used when you want the features of an existing class inside your new class, but not its interface. That is, you embed an object to implement features of your new class, but the user of your new class sees the interface you’ve defined rather than the interface from the original class. In the given c++ classes, class triangle is defined using Composition by using point class objects. The selected area in the following picture is a class defined using composition.

1 /* FILE: ./shapes7/triangle.h 2 #include <iostream> 3 #include point.h 4 using std::ostream; 5. class triangle{ 6 point v

7. A class defined using Inheritance

In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. In the given c++ classes, class circle is defined using Inheritance from point class. The selected area in the following picture is a class defined using Inheritance.

1- /* W NA 9 3 FILE: -/shapes7/circle.h 4 #include <iostream> 5 #include <cmath> 6 #include point.h 7 using std::ostream; 8

8. A parameter or return value that is a reference

When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration. In the given c++ classes, there are parameters references in class triangle, class circle and class shape. But the selected area in the following image is only one instane of it, in triangle class.

1 /* FILE: ./shapes7/triangle.h 2 #include <iostream> 3 #include point.h 4 using std::ostream; 5. class triangle{ 6 point v

9. Operator Overloading

You can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with user-defined types as well.

Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

Operator overloading happened in classes triangle, circle, shape. The selected area in the following image is the instance of Operator Overloading in class triangle.

1 /* FILE: -/shapes7/triangle.h */ 2 #include <iostream> 3 #include point.h 4 using std::ostream; 5. class triangle{ 6 poin

If you have any doubts, ask me in the comment box.

If you like my work and explanation, give me a like and feedback. That helps me a lot. Thank you.

All the best.

Add a comment
Know the answer?
Add Answer to:
/* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the...

    create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not be modified. Instructions ar egiven below. Shape class The Shape class is an abstract base class from which Rectangle and Circle are derived. bool fits_in(const Rectangle& r) is a pure virtual function that should return true if the Shape fits in the Rectangle r. void draw(void) is a pure virtual function that writes the svg...

  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

  • C++ myStack.lh stackADT.h Instructions main.cpp 1 #include«iostream» 2 #include<stdlib.h> 3 #include«conio.h> 4 #include«ostream» 5 using namespace std; 6 const int SIZE-5; //Stack size 7 //...

    C++ myStack.lh stackADT.h Instructions main.cpp 1 #include«iostream» 2 #include<stdlib.h> 3 #include«conio.h> 4 #include«ostream» 5 using namespace std; 6 const int SIZE-5; //Stack size 7 //class declaration 8 class stack Instructions Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same Overload the relational operatorfor the class stackType that returns true if two stacks of the same type are the same; it returns false...

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,    double stArea, int yAdm, int oAdm) {    stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } void stateData::getStateInfo(string& sName, string& sCapital,    double& stArea, int& yAdm, int& oAdm) {    sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } string stateData::getStateName() { return stateName;...

  • lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream;...

    lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream; enum Token { // keywords PRINT, BEGIN, END, IF, THEN, // an identifier IDENT, // an integer and string constant ICONST, RCONST, SCONST, // the operators, parens, semicolon PLUS, MINUS, MULT, DIV, EQ, LPAREN, RPAREN, SCOMA, COMA, // any error returns this token ERR, // when completed (EOF), return this token DONE }; class LexItem { Token token; string lexeme; int lnum; public: LexItem()...

  • Thanks in advance: // File: main.cpp #include <iostream> #include <fstream> #include <iomanip> using namespace std; int...

    Thanks in advance: // File: main.cpp #include <iostream> #include <fstream> #include <iomanip> using namespace std; int recursiveCount = 0; void triangle(const char drawChar, const int maxHeight, const int currentHeight); int main() { char drawChar = '*'; recursiveCount = 0;    cout.fill(drawChar); triangle(drawChar, 5, 1); cout.fill(' '); return 0; }// end main() void triangle(const char drawChar, const int maxHeight, const int currentHeight) { /* DO NOT edit code */ cout.fill(drawChar); recursiveCount += 1; /* END of do not edit */ /*...

  • Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass...

    Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...

  • I need to implement a program that requests a x,y point and the radius of a...

    I need to implement a program that requests a x,y point and the radius of a circle. then it figures out the area and circumference using an overloaded operation. The full instructions, what I have and the errors I have are below. A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, pointType, that can store and process a point in the x-y plane. You should then perform operations on the point, such as...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;   ...

    //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;    double mpg;    Warranty warranty;    static int numOfVehicles; public:    Vehicle();    Vehicle(string s, int y, double m, Warranty warranty);    Vehicle(Vehicle& v);    ~Vehicle();    string getMake();    int getYear();    double getGasMileage();    void setMake(string s);    void setYear(int y);    void setYear(string y);    void setGasMileage(double m);    void setGasMileage(string m);    void displayVehicle();    static int getNumVehicles();    Warranty getWarranty();    void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() {    make = "unknown";    year =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT