473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

I have Warning and the result after debugging is rubbish values

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Circle_computations
  5. {
  6.  
  7. protected:
  8.     double area,circumference,radius,pi;
  9.  
  10. public:
  11.  
  12.  
  13.  Circle_computations()
  14. {
  15.  }    
  16.   Circle_computations(double radi)
  17. {
  18. pi = 3.14;
  19.  
  20. set_r(radi);
  21.  
  22. }
  23.  
  24.  
  25.  
  26.   double set_r(double radi)
  27.   {
  28.  
  29. try
  30. {
  31. if(radi<=0.0)
  32. {
  33. throw "PLESE ENTER positive NUMBERS";
  34.  
  35. }
  36.  
  37. radius=radi;
  38.  
  39.  
  40. }
  41.  
  42. catch ( const char* strException )
  43.     {
  44.         cerr << "Error: " << strException << "\n";
  45.         return 1 ;
  46.  
  47.     }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.   }
  57.  
  58. double get_r()
  59. {
  60.  
  61. return radius;
  62.  
  63. }
  64.  
  65.  
  66.  
  67. double circle_area()
  68. {
  69.  
  70. area = pi*radius*radius;
  71. return area;
  72.  
  73. }
  74.  
  75. double circle_circumferunce()
  76. {
  77.  
  78. circumference =2*pi*radius;
  79. return circumference;
  80.  
  81. }
  82. void print_circleoutput()
  83. {
  84. cout<<"****The  Area OF The Circle  is****\n"<<area<<"\n";
  85.  
  86. cout<<"****The Circle Circumferunce is****\n"<<circumference<<"\n";
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94. };
  95.  
  96. class Cylinder:public Circle_computations
  97. {
  98.  
  99. protected:
  100.     double height,volume,cyarea;
  101.  
  102. public:
  103. Cylinder(double r, double h) :
  104.     Circle_computations(r)
  105. {
  106.     set_height(h);
  107. }
  108.  
  109.  
  110.  
  111. double set_height(double h)
  112.   {
  113.  
  114. try
  115. {
  116. if(h<=0.0)
  117. {
  118. throw "PLESE ENTER positive NUMBERS";
  119.  
  120. }
  121.  
  122. height=h;
  123.  
  124.  
  125. }
  126.  
  127. catch ( const char* strException )
  128.     {
  129.         cerr << "Error: " << strException << "\n";
  130.         return 1 ;
  131.  
  132.     }
  133.  
  134.  
  135.  
  136.  
  137.   }
  138.  
  139. double get_height()
  140. {
  141.  
  142. return height;
  143.  
  144. }
  145.  
  146.  
  147.  
  148.     double cylindersurface_area()
  149. {
  150.     cyarea=(2 * (Circle_computations::circle_area()))+( circle_circumferunce()* height) ;
  151.  
  152.     return cyarea;
  153.  
  154. }
  155.  
  156.  
  157. double c_volume()
  158. {
  159.     volume = (Circle_computations::circle_area())* height;
  160.  
  161.     return volume;
  162.  
  163. }
  164.  
  165.  
  166. void print_cylinderoutput()
  167. {
  168. cout<<"****The cylinder surface  Area   is****\n"<<cyarea;;
  169.  
  170. cout<<"****The cylinder volume   is****\n"<<volume;
  171. }
  172.  
  173.  
  174. };
  175.  
  176.  
  177. int main()
  178.  
  179. {
  180.  
  181. Circle_computations  compute(1.5);
  182. Cylinder computecylider(1.5,2.5);
  183. cout<<"****The  Radius  is****\n";
  184.  
  185. computecylider.get_height();
  186. computecylider.get_r();
  187. computecylider.set_r(3.5);
  188. computecylider.set_height(2.6);
  189. computecylider.print_cylinderoutput();
  190.  
  191.  
  192. }
Oct 30 '13 #1
1 1103
weaknessforcats
9,208 Expert Mod 8TB
First, the inheritance is not required. A cylinder is not a circle. A cylinder contains a circle. That is to say a cylinder HAS-A circle. The HAS-A relationship is implemented using a circle member variable in the cylinder.

You can pass the height an diameter of the cylinder to the cylinder methods who in turn pass the diameter to circle methods that maintain the circle object inside the cylinder object.

Second, do not use protected unless you know what protected is for. This is not it. You need protected data members when developing a derived class using multiple bass classes. That is, by using multiple inheritance. In this problem all member data is private and all class methods are public.

Using protected here is an attempt to do and end run on data security by avoiding private access. This violates the encapsulation and data hiding rules of object programming.

If you want the area of the ends of the cylinder then call a cylinder method that returns the area. The cylinder method just calls the circle area method using the private circle object that is inside the cylinder.

Third, write your circle class by itself and get it working. Then write your cylinder class to use the already working circle class. You will find your data issues disappear.

BTW: Don't forget to write constructors to initialize your data members to avoid garbage values.
Oct 30 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David Stephenson | last post by:
Hello list, When using 'decode' is it possible to combine 2 different search values into 1 e.g decode(a.country, 'CH', di.identity,'JA', di.identity, de.identity) it would be nice to say...
43
by: Anitha | last post by:
Hi I observed something while coding the other day: if I declare a character array as char s, and try to use it as any other character array..it works perfectly fine most of the times. It...
1
by: Nuno Morgadinho | last post by:
Hello all, I'm messing around with the Server Programming Interface and the particular example presented at: http://www.postgresql.org/docs/current/interactive/spi-examples.html Ideally, I...
8
by: Charles Sullivan | last post by:
I have a program written in C under Linux (gcc) which a user has ported to run under AT&T SysV R4. He sent me a copy of his makelog which displays a large number of compiler warnings similar to...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
5
by: j1o1h1n | last post by:
Hello, I was trying to create a flattened list of dictionary values where each value is a list, and I was hoping to do this in some neat functionally style, in some brief, throwaway line so that...
13
by: Gregor =?UTF-8?B?S292YcSN?= | last post by:
Hi! With VALUES you can do something like: SELECT * FROM (VALUES ('A', 1), ('B', 2), ('C', 2)) AS TEMP(LETTER, NUMBER) which will give you: LETTER NUMBER ------ ------ A 1 B 2...
3
by: Dipti Singh | last post by:
Hi All, Please help me, I am trying it from 2 days. I have a login page in asp that is including other pages using #include..... after login user in the same page but enters in the database. I...
3
by: siyaverma | last post by:
i am trying to upload csv file from user's computer to main server the code i am using is if(((isset($_GET)) && ($_GET=="yes")) ) { $typefield = $_GET; echo...
3
by: JRough | last post by:
I want to save two variables in a $_SESSION for use in another page: $_SESSION = $mark; $_SESSION = $num; then on the other page I did this to get the value: $mark =$_SESSION; $num =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.