Jun 3, 2009 at 6:27pm UTC
could someone pls help me to make some changes in this code
, instead of CIRCLE and CONUS I want it to function for Triangle and Pyramid...also include this polymorphism concepts:
1) virtual function
2) pure virtual functions
3) main program:
i) base class pointer-> base class object
ii) derived class pointer-> base class object
this is the code:
circle1.h
// abstract base class circle
#ifndef CIRCLE1_H
#define CIRCLE1_H
class Circle{
public:
Circle(double =0, double =0, float =0);
virtual float volume() = 0;//pure virtual function
virtual void print();
void set(double,double, float);
float perimeter();
float surface ();
float getx();
float gety();
float getR();
private:
double x,y;
float r;
};
#endif
circle1.cpp
#include<iostream>
#include<cmath>
using namespace std;
#include "circle1.h"
const float pi=3.14;
Circle::Circle(double xx, double yy, float rr)
{
set(xx,yy, rr);
}
// print()
void Circle::print()
{
cout<<"\n Centar so koordinati ("<<getx()<<","<<gety()<<
")"<<endl; // -direktno x, y moze: x i y
cout<<"\nRadius r="<<r
<<"\nPerimeter P="<<perimeter()
<<"\nSurface S="<<surface()
<<endl;}
float Circle::perimeter()
{
return 2*r*pi ;
}
float Circle::surface()
{
return r*r*pi;}
float Circle:: getx()
{return x;}
float Circle:: gety()
{return y;}
float Circle:: getR()
{return r;}
void Circle::set(double xx, double yy, float rr)
{
//C.x=xx; nuk mundet sepse eshte private
//C.y=yy;nuk mundet sepse eshte private
x=xx; y= yy;
r=rr;
}
conus.h
#ifndef CONUS_H
#define CONUS_H
#include "circle1.h"
class Conus : public Circle{
private:
float h;
public:
Conus(double =0.0, double =0.0, float =0.0, float =0.0);
void set(double x1, double y1, float r1, float h1);
float geth();
float surface();
virtual float volume();
virtual void print();
};
#endif
conus.cpp
#include <iostream>
#include <cmath>
using namespace std;
#include "conus.h"
#include "circle1.h"
Conus::Conus(double x1, double y1, float r1, float h1):Circle(x1,y1,r1)
{h=h1;}
void Conus::set(double x1, double y1, float r1, float h1)
{
Circle::set(x1,y1,r1);
h=h1;
}
float Conus::geth(){return h;}
float Conus::surface()
{
return Circle::surface()+perimeter()*h;
}
float Conus::volume()
{
return Circle::surface()*h/3;
}
void Conus::print()
{
cout<<"\n Conus so osnova so :";
Circle::print();
cout<<"\n Visina="<<geth()
<<"\n Polshtina na conus"<<surface()
<<"\n Volumen ="<<volume()
<<endl;
}
main.cpp
#include <iostream>
#include <cmath>
using namespace std;
#include "circle1.h"
#include "conus.h"
int main()
{
Circle *Cptr=0;
Conus K, *Kptr=0;
K.set(3,5,4.5,6);
Circle C
Kptr=&K;
K.print();
Kptr->print();
Cptr->print();
cin.get();
return 0;
}
Jun 3, 2009 at 8:53pm UTC
@Disch
There is an error in your code, line 5 should be double ridiculefactor = numeric_limits<double >::infinity();