Game Programming




 
game programming
Game Programming :: Programming :: General Programming :: Code Examples :: [C++]Creating and using objects - View Topic
Topic Rating: *****
Printable View
Andy
Administrator
*****

Posts: 40
Status: Offline
Gender: Male
Joined:  
pm

Reputation: 0%

[C++]Creating and using objects (13th Jan 08 at 12:31am UTC)
So what we have here is a simple program that will be able to tell if the width and height is equal to a rectangle or a square

In the program i have used a Class called CRectangle

and the object i have used is called myRectangle


Code:
 
  1. #include <iostream>
  2.  
  3. //Class decliration
  4. class CRectangle {
  5.  
  6. public:
  7.  
  8. //declare the attributes
  9. unsigned width, height;
  10.  
  11. //declare the methods
  12. void setSize(unsigned x, unsigned y);
  13. unsigned area();
  14. unsigned prim();
  15. bool isSquare();
  16.  
  17.  
  18. };//dont forget semicolon
  19.  
  20. /*
  21. The Method setSize() assings the
  22. rectangles dimesions to the
  23. attributes
  24. it takes two arguments both unsigned
  25. intergers
  26. */
  27. void CRectangle::setSize(unsigned x, unsigned y)
  28. {
  29. width = x;
  30. height = y;
  31.  
  32. }
  33.  
  34. /*
  35. The method area() returns the
  36. rectangles area
  37. it takes two arguments both floats
  38. it returns a float
  39. */
  40. unsigned CRectangle::area()
  41. {
  42. return(width * height);
  43.  
  44. }
  45.  
  46. /*
  47. the method prim() returns the
  48. rectangles perimiter
  49. it takes two arguments, both unsigned
  50. intergers
  51. it returns an unsigned interger
  52. */
  53. unsigned CRectangle::prim()
  54. {
  55. return(width + width + height + height);
  56.  
  57. }
  58.  
  59. /*
  60. The method isSquare() returns a
  61. boolean value indicating wheather
  62. or not the rectangle is also a square
  63. it takes two arguments both unsigned
  64. intergers
  65. it returns a boolean
  66. */
  67. bool CRectangle::isSquare()
  68. {
  69. if(width == height)
  70. {
  71. return true;
  72.  
  73. }else{
  74. return false;
  75.  
  76. }
  77. }
  78.  
  79.  
  80.  
  81. int main()
  82. {
  83. unsigned width = 25;
  84. unsigned height = 14;
  85.  
  86. std::cout << "With a width of "
  87. << width << " and a height of "
  88. << height << "nn";
  89.  
  90. //Create a new Object called myRectangle;
  91. CRectangle myRectangle;
  92.  
  93. //assing values
  94. myRectangle.setSize(width, height);
  95.  
  96. //print area
  97. std::cout << "The area of the rectangle is "
  98. << myRectangle.area() << "nn";
  99.  
  100. //print prim
  101. std::cout << "The prim of the rectangle is "
  102. << myRectangle.prim() << "nn";
  103.  
  104. //is a sqare
  105. std::cout << "This rectangle ";
  106. if(myRectangle.isSquare())
  107. {
  108. std::cout << "Is also ";
  109. }else{
  110. std::cout << "Is not ";
  111.  
  112. }
  113. std::cout << " A square nn";
  114.  
  115. std::cin.get();
  116.  
  117. return 0;
  118.  
  119. }
  120.  
 
 Printable View

All times are GMT+0 :: The current time is 3:19pm
Page generated in 0.9702 seconds
Purchase Ad-Free Credits
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums