Aggregation

How do I pass an argument to a constructor that has a class has its parameter?

The print method should only print the radius and height of the cyllinder
That is, use the print method in the Circle class.
Then test the Circle class
with 2 cylinder objects, one with two values circle and height and one from an existing cyllinder using the copy constructor.

Then print the radius height
of each object via the print method

Also print surface area and volume of each object via the get method.



#include <iostream>
//#include "circleType.h"
//#include "circle"
using namespace std;

class circle {

private:
double radius;
double area;

public:
circle();
circle(double );
circle (circle &);

void setRadius(double );
double getRadius();
double getArea();
void print();

// static double PI;
double static PI;

};

circle ::circle(){

radius = 5;
}

circle :: circle (double c){

radius = 8;
}


circle :: circle (circle &existingCircle){

radius = existingCircle.radius;
area = existingCircle.area;

}

double circle:: PI = 3.14;

void circle:: setRadius (double rad){
radius = rad;
}

double circle:: getRadius(){
return radius;
}


double circle:: getArea(){
area = PI * (radius * radius);
return area;
}

void circle :: print(){

cout << "The radius is " << radius << endl;
cout << "The area is " << area << endl;
}

// ------------------------------------------------------

class cyllinder {

private:
double height;
circle base;
//ADD CRICLE BASE;


public:

//DONT FORGET CIRRCLE DATATYPE AS AN ARGUMENT
cyllinder(circle, double);

cyllinder(cyllinder &);

void setHeight(double);
double getHeight();
double getArea();
double getVolume();
void print();

};


// CONSTRUCTORS!!!!!!!!!!!


cyllinder :: cyllinder(circle r,double x){
base = r;

if (x >= 0)
height = x;
else
height = 0;

}

cyllinder :: cyllinder (cyllinder &existing){
height = existing.height;
base = existing.base; // its copying the rectangle info to it??

}

void cyllinder :: setHeight(double y){
height = y;
}

double cyllinder:: getHeight(){
return height;
}

double cyllinder :: getArea(){
return base.getArea();
}

double cyllinder :: getVolume(){
return (height * base.getArea());
}

void cyllinder :: print(){
base.print();

}


int main() {

// cyllinder object1(x,y);
double x = 5;
double y = 6;
double z = 2.5;

//Why do have declare their datatypes?? and this works but I haven't declared
//another circle for height is that not necessary?
cyllinder object1(circle x, double z);





}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

struct circle
{
    double radius ;

    // explicit: we don't want this to be a converting constructor
    //           https://en.cppreference.com/w/cpp/language/converting_constructor
    // member initializer list: https://en.cppreference.com/w/cpp/language/initializer_list
    explicit circle( double r = 5 ) : radius( r<0 ? 0 : r ) {}

    // const: https://isocpp.org/wiki/faq/const-correctness#const-member-fns
    void print() const { std::cout << "circle with radius " << radius << '\n' ; }
};

struct cylinder
{
    circle base ;
    double height ;

    explicit cylinder( circle base = circle(1), double height = 1 ) ;

    void print() const ;
};

cylinder::cylinder( circle b, double h) : base(b), height( h<0 ? 0 : h ) {}

void cylinder::print() const
{
    std::cout << "cylinder\nbase: " ;
    base.print() ;
    std::cout << "height " << height << "\n\n" ;
}

int main()
{
    const circle c(1.9) ;
    const double h = 0.7 ;
    
    // construct a cylinder with base c and height h
    const cylinder one( c, h ) ;
    one.print() ;
    
    // construct a cylinder with unnamed objects for base and height
    const cylinder two( circle(2.3), 4.6 ) ;
    two.print() ;
}

http://coliru.stacked-crooked.com/a/0d6feb0cc690d7a7
Topic archived. No new replies allowed.