Hi!

This is just an easy example of class inheritance. But, why does Sphere(Circle& K):Circle(K){} work? What does Circle(K) mean?
Thanks!

#include<iostream>
#define PI 3.14159
using namespace std;

class Circle
{
    protected:
        float r;

    public:
        Circle(){}
        Circle(float r):r(r){}

        float circumference() {return 2*r*PI;}
        float area() {return r*r*PI;}
};

class Sphere: public Circle
{
    public:
        Sphere(){}
        Sphere(float x):Circle(x){}
        Sphere(Circle& K):Circle(K){}

        float volume() {return 4/3.0*r*r*r*PI;}
        float surface() {return 4*r*r*PI;}
};

int main()
{
    Circle K1(4);
    Sphere K2(K1);

    cout << K2.volume();

    return 1;
}

Sphere(Circle& K):Circle(K){}

yoy make Sphere class's constructor and in its parameter list you add address of Circle class object, where K is the object of Circle class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.