Click here to Skip to main content
16,016,814 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: IDirect3DSurface9 Problem after Windows 10 Update Creator Pin
Jochen Arndt25-Jul-17 20:57
professionalJochen Arndt25-Jul-17 20:57 
GeneralRe: IDirect3DSurface9 Problem after Windows 10 Update Creator Pin
002comp25-Jul-17 21:19
002comp25-Jul-17 21:19 
QuestionC++ ActiveX project worked with VB6, broken in later versions Pin
Burl Rice22-Jul-17 10:30
Burl Rice22-Jul-17 10:30 
AnswerRe: C++ ActiveX project worked with VB6, broken in later versions Pin
Richard MacCutchan22-Jul-17 20:29
mveRichard MacCutchan22-Jul-17 20:29 
GeneralRe: C++ ActiveX project worked with VB6, broken in later versions Pin
Burl Rice23-Jul-17 2:32
Burl Rice23-Jul-17 2:32 
GeneralRe: C++ ActiveX project worked with VB6, broken in later versions Pin
Richard MacCutchan23-Jul-17 2:54
mveRichard MacCutchan23-Jul-17 2:54 
GeneralRe: C++ ActiveX project worked with VB6, broken in later versions Pin
Burl Rice23-Jul-17 9:12
Burl Rice23-Jul-17 9:12 
QuestionClass Shape Pin
kumn1d22-Jul-17 9:05
kumn1d22-Jul-17 9:05 
This is the output I am supposed to get but I am getting errors please help me

C++
int main()
{
	int size = 5;
	
	Shape** shapes = new Shape*[size];

	Point* centre = new Point(5,5);
	Rectangle* rectangle1 = new Rectangle(*centre, 4, 3);
	shapes[0] = rectangle1;
	
	shapes[1] = new Rectangle (*new Point(10, -10), 4, 4);

	Rectangle* rectangle3 = new Square(*new Point(-8.5,-10), 2.5);
	shapes[2] = rectangle3;

	Square* square2 = new Square (*new Point(-1,-1), 1.5);
	shapes[3] = square2;
	
	Circle* circle = new Circle(*new Point(100,100), 25.4);
	shapes[4] = circle;

	cout << "\n11111111111111111111111111\n";
 
	for (int i = 0; i < size; ++i)
		shapes[i] -> display();

	cout << "\n22222222222222222222222222\n";

	cout << "\nthe area of the first rectangle is: ";
	cout << rectangle1->area() << endl;

	cout << "It is a square, the area is: ";
	Square* square = dynamic_cast<Square*> (rectangle3);
	cout << square->area() << endl;	

	cout << "the perimeter of the circle is: ";
	cout << circle->perimeter() << endl;	

	cout << "\n33333333333333333333333333\n";

	cout << "\nThe four vertices of the first rectangle are: (clockwise from top-left)\n";
	rectangle1->printFourVertices();

	cout << "\nThe four vertices of the third rectangle are:\n";
	rectangle3->printFourVertices();

	cout << "\n44444444444444444444444444\n";

	rectangle1->reflectX();

	shapes[2] -> translate(1.5, 3);

	shapes[4] -> translate(-100,-100);

	cout << "\nAfter reflection and translation, here are the moved shapes:\n" ;
	for (int i = 0; i < size; ++i)
		shapes[i] -> display();
	
	cout << "\n55555555555555555555555555\n";
 
	cout << "\nNow, the four vertices of the first rectangle are:\n";
	rectangle1->printFourVertices();
 
	cout << "\nNow, the four vertices of the third rectangle are:\n";
	rectangle3->printFourVertices();

	cout << "\n66666666666666666666666666\n\n";
 
	for (int i = 0; i < size; ++i)
		cout << shapes[i] -> area() << endl;

	// From the following statements, understand how setLength() and setWidth()
	// work for rectangles and squares.

	cout << "\n777777777777777777777777777\n";

	shapes[1] -> display();
	dynamic_cast<Rectangle*> (shapes[1]) -> setLength(8.2);
	cout << "\nAfter setLength(8.2), the rectangle is: ";
	shapes[1] -> display();
	dynamic_cast<Rectangle*> (shapes[1]) -> setWidth(5);
	cout << "\nAfter setWidth(5), the rectangle is: ";
	shapes[1] -> display();

	square2 -> display();
	square2 -> setLength(8.2);
	cout << "\nAfter setLength(8.2), the rectangle is: ";
	square2 -> display();
	square2 -> setWidth(5);
	cout << "\nAfter setWidth(5), the rectangle is: ";
	square2 -> display();

	system("pause");
	return 0;
}


/*
11111111111111111111111111

Rectangle:
        Centre: (5, 5)
        Length: 4
        Width: 3

Rectangle:
        Centre: (10, -10)
        Length: 4
        Width: 4

Square:
        Centre: (-8.5, -10)
        Side Length: 2.5

Square:
        Centre: (-1, -1)
        Side Length: 1.5

Circle:
        Centre: (100, 100)
        Radius: 25.4

22222222222222222222222222

the area of the first rectangle is: 12
It is a square, the area is: 6.25
the perimeter of the circle is: 159.512

33333333333333333333333333

The four vertices of the first rectangle are: (clockwise from top-left)
(3, 6.5)
(7, 6.5)
(7, 3.5)
(3, 3.5)

The four vertices of the third rectangle are:
(-9.75, -8.75)
(-7.25, -8.75)
(-7.25, -11.25)
(-9.75, -11.25)

44444444444444444444444444

After reflection and translation, here are the moved shapes:

Rectangle:
        Centre: (5, -5)
        Length: 4
        Width: 3

Rectangle:
        Centre: (10, -10)
        Length: 4
        Width: 4

Square:
        Centre: (-7, -7)
        Side Length: 2.5

Square:
        Centre: (-1, -1)
        Side Length: 1.5

Circle:
        Centre: (0, 0)
        Radius: 25.4

55555555555555555555555555

Now, the four vertices of the first rectangle are:
(3, -3.5)
(7, -3.5)
(7, -6.5)
(3, -6.5)

Now, the four vertices of the third rectangle are:
(-8.25, -5.75)
(-5.75, -5.75)
(-5.75, -8.25)
(-8.25, -8.25)

66666666666666666666666666

12
16
6.25
2.25
2025.8

777777777777777777777777777

Rectangle:
        Centre: (10, -10)
        Length: 4
        Width: 4

After setLength(8.2), the rectangle is:
Rectangle:
        Centre: (10, -10)
        Length: 8.2
        Width: 4

After setWidth(5), the rectangle is:
Rectangle:
        Centre: (10, -10)
        Length: 8.2
        Width: 5

Square:
        Centre: (-1, -1)
        Side Length: 1.5

After setLength(8.2), the rectangle is:
Square:
        Centre: (-1, -1)
        Side Length: 8.2

After setWidth(5), the rectangle is:
Square:
        Centre: (-1, -1)
        Side Length: 5
*/


This is what I have tried
C++
// jianger.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
# include<iostream>
# include<cmath>
# include<string>

using namespace std;
class Point
{
private:
	double x;
	double y;
public:
	Point();
	Point(const double &x,const double &y);
	void setX(const double &x);
	void setY(const double &y);
	double getX()const;
	double getY()const;
	double DisFromOrigin()const;
	virtual void translate(const double &xDis, const double &yDis);
	void reflectX(); // chnage my private members
	void reflectY();
	virtual void PrintName()const;
};
Point::Point()
{
	x=0;
	y=0;
}
Point::	Point(const double &x,const double &y)
{
	this->x=x;
	this->y=y;
}
void  Point::setX(const double &x)
{
	this->x=x;
	this->y=y;
}
void Point:: setY(const double &y)
{
	this->y =y;
}
double Point:: getX()const
{
	return x;
}
double Point:: getY()const
{
	return y;
}
double  Point::DisFromOrigin()const
{
	return (sqrt(x*x + y*y));
}
void Point:: translate(const double &xDis, const double &yDis)
{
	double a;
	double b;
	a = this->x + xDis;
	b= this->y + yDis;
	this->x= a;
	this->y=b;
}
void Point::reflectX()
{
	this->y = -1 *y;
}
void Point::reflectY()
{
	this->x= -1*x;
}
void Point::PrintName()const
{
	cout<<"Point ("<<getX()<<" , "<<getY()<<")"<<endl;
}
class Shape
{
public:
	virtual double Peri()const=0;
	virtual void PrintName()const=0;
	virtual void translate(double x1,double y1)=0;
	virtual void reflectX()=0;
	virtual void reflectY()=0;
	virtual double getArea()const=0;
	virtual double getPeri()=0;
	virtual string getName()const=0;
}
class Circle:public Shape
{

public:
	Circle();
	Circle(const Point &p, const double &r);
	double getR()const;
	void setR(const double &r);
	virtual double getPeri()const;
    virtual void PrintName()const;
	virtual void translate(const double &x1, const double  &y1);
	virtual void reflectX();
    virtual void reflectY();
    virtual double getArea()const;
   virtual string getName()const;
protected:
	Point center;
	string name;
	double radius;
	 

};
Circle::Circle()
{
	name = "Circle";
	center = Point(0,0);
	radius = 1;
}
Circle::Circle(const Point &p, const double &r)
{
	name = "Circle";
	this->center= p;
	this->radius=r;
}

double Circle::getPeri()const
{
	return (2*3.14*getR());
}
double Circle::getR()const
{
	return radius;
}
void Circle::setR(const double &r)
{
	this->radius = r;
}
 void Circle:: PrintName()const
 {
	 cout<<"Circle"<<endl;
 }
 void Circle::translate(const double &x1, const double &y1)
 {
	 this->center.translate(x1 , y1);
 }
void Circle::reflectX()
{
	this->center.reflectX();
}
void Circle::reflectY()
{
	this->center.reflectY();
}
double Circle::getArea()const
{
	return (3.14*this->getR()*this->getR());
}
string Circle::getName()const
{
	return this->name;
}
class Rectangle:public Shape
{
private:
	double len;
	double width;
	Point center;
	string name;
public:
	Rectangle();
	Rectangle(const Point &p, const double &len , const double &width);
	void setLen(double len);
	void setWid(double width);
	double getLen()const;
	double getWid()const;
	virtual double getPeri()const;
	virtual void PrintName()const;
	virtual void translate(const double &x1, const double  &y1);
	virtual void reflectX();
	virtual void reflectY();
	virtual double getArea()const;
	virtual string getName()const;
};
Rectangle::Rectangle()
{
	len =1;
	width=1;
	this->name = "Rectangle";
	center = Point(0,0);
}

Rectangle::	Rectangle(const Point &p, const double &len , const double &width)
{
	this->name ="Rectangle";
	this->center = p;
	this->len = len;
	this->width = width;
}
double Rectangle:: getPeri()const
{
	return (2*getLen() + 2*getWid());
}
void Rectangle::PrintName()const
{
	cout<<"Rectangle"<<endl;
}
void Rectangle::translate(const double &x1, const double  &y1)
{
	this->center.translate(x1, y1);
}
void Rectangle::reflectX()
{
	this->center.reflectX();
}
void Rectangle::reflectY()
{
	this->center.reflectY();
}
double Rectangle::getArea()const
{
	return(getLen()*getWid());
}
string Rectangle::getName()const 
{
	return name;
}
void Rectangle::setLen(double len)
{
	this->len = len;
}
void Rectangle::setWid(double width)
{
	this->width = width;
}
double Rectangle::getLen()const
{
	return len;
}
double Rectangle::getWid()const
{
	return width;
}
class Square:public Rectangle
{
private:
	Point center;
	string name;
	double side;
public:
	Square();
	Square(const double &side);
	void setSide(const double &side);
	double getSide()const;
	virtual double getPeri()const;
	virtual void PrintName()const;
	virtual void translate(const double &x1, const double  &y1);
	virtual void reflectX();
	virtual void reflectY();
	virtual double getArea()const;
	virtual string getName()const;
};
Square::Square()
{
	side=0;
}
Square::Square(const double &side)
{
	this->side = side;
}
void Square::setSide(const double &side)
{
	this->side = side;
	this->Rectangle::setLen(side);
	this->Rectangle::setWid(side);
}
double Square::getSide()const
{
	return this->Rectangle::getLen();
}
double Square:: getPeri()const
{
	return(this->Rectangle::getLen()* 4);
}
void Square::PrintName()const
{
	cout<<"Square"<<endl;
}
void Square::translate(const double &x1, const double  &y1)
{
	this->Rectangle::translate(x1, y1);
}
void Square::reflectX()
{
	this->Rectangle::reflectX();
}
void Square::reflectY()
{
	this->Rectangle::reflectY();
}
double Square::getArea()const
{
	return(this->Rectangle::getLen()* this->Rectangle::getLen());
}
string Square::getName()const
{
	return name;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int size = 5;
	
	Shape** shapes = new Shape*[size];

	Point* centre = new Point(5,5);
	Rectangle* rectangle1 = new Rectangle(*centre, 4, 3);// it means that this can not be abstract class!!! coz it is calling object of itself so no virtual functions
	shapes[0] = rectangle1;
	
	shapes[1] = new Rectangle (*new Point(10, -10), 4, 4);

	Rectangle* rectangle3 = new Square(*new Point(-8.5,-10), 2.5);
	shapes[2] = rectangle3;

	Square* square2 = new Square (*new Point(-1,-1), 1.5);
	shapes[3] = square2;
	
	Circle* circle = new Circle(*new Point(100,100), 25.4);
	shapes[4] = circle;

	cout << "\n11111111111111111111111111\n";
 
	for (int i = 0; i < size; ++i)
		shapes[i] -> display();

	return 0;
}


modified 22-Jul-17 15:46pm.

AnswerRe: Class Shape Pin
Afzaal Ahmad Zeeshan22-Jul-17 10:00
professionalAfzaal Ahmad Zeeshan22-Jul-17 10:00 
AnswerRe: Class Shape Pin
_Flaviu23-Jul-17 23:42
_Flaviu23-Jul-17 23:42 
GeneralRe: Class Shape Pin
Richard MacCutchan23-Jul-17 23:58
mveRichard MacCutchan23-Jul-17 23:58 
AnswerRe: Class Shape Pin
Victor Nijegorodov24-Jul-17 22:38
Victor Nijegorodov24-Jul-17 22:38 
GeneralRe: Class Shape Pin
kumn1d25-Jul-17 8:52
kumn1d25-Jul-17 8:52 
QuestionRe: Class Shape Pin
David Crow25-Jul-17 2:23
David Crow25-Jul-17 2:23 
AnswerRe: Class Shape Pin
kumn1d25-Jul-17 8:51
kumn1d25-Jul-17 8:51 
QuestionRe: Class Shape Pin
David Crow25-Jul-17 10:15
David Crow25-Jul-17 10:15 
SuggestionEducational: my buffer scroll C++ working code. Pin
Member 132416522-Jul-17 0:31
Member 132416522-Jul-17 0:31 
GeneralRe: Educational: my buffer scroll C++ working code. Pin
Richard MacCutchan22-Jul-17 0:57
mveRichard MacCutchan22-Jul-17 0:57 
GeneralRe: Educational: my buffer scroll C++ working code. Pin
Member 132416522-Jul-17 5:19
Member 132416522-Jul-17 5:19 
GeneralRe: Educational: my buffer scroll C++ working code. Pin
leon de boer22-Jul-17 1:14
leon de boer22-Jul-17 1:14 
GeneralRe: Educational: my buffer scroll C++ working code. Pin
Member 132416522-Jul-17 5:09
Member 132416522-Jul-17 5:09 
QuestionCN Pin
Member 1332222821-Jul-17 7:58
Member 1332222821-Jul-17 7:58 
AnswerRe: CN Pin
Afzaal Ahmad Zeeshan21-Jul-17 9:12
professionalAfzaal Ahmad Zeeshan21-Jul-17 9:12 
AnswerRe: CN Pin
jeron121-Jul-17 9:34
jeron121-Jul-17 9:34 
AnswerRe: CN Pin
David Crow21-Jul-17 10:30
David Crow21-Jul-17 10:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.