anyone can help me for "Try and Catch" method?
Mar 28, 2012 at 2:12am UTC
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <string>
using namespace std;
#ifndef _FRUIT_H
#define _FRUIT_H
class Fruit
{
public :
Fruit(string name, string color, int weight);
string getName();
void setName(string name);
string getColor();
void setColor(string color);
int getWeight();
void setWeight(int weight);
protected :
string name;
string color;
int weight;
};
Fruit::Fruit(string n, string c, int w)
{
name = n;
color = c;
weight = w;
};
string Fruit::getName()
{
return name;
};
void Fruit::setName(string n)
{
name = n;
};
string Fruit::getColor()
{
return color;
};
void Fruit::setColor(string c)
{
color = c;
};
int Fruit::getWeight()
{
return weight;
};
void Fruit::setWeight(int w)
{
weight = w;
};
#endif
1 2 3 4 5 6 7 8 9 10
#include "Fruit.h"
class Mango : public Fruit
{
public :
Mango() : Fruit("Mango" , "Green" , 10)
{
}
};
1 2 3 4 5 6 7 8 9 10
#include "Fruit.h"
class Banana : public Fruit
{
public :
Banana() : Fruit("Banana" , "Yellow" , 5)
{
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
#include "Banana.h"
#include "Mango.h"
using namespace std;
int main(void )
{
Banana banana;
Mango mango;
cout << "Fruit: " << banana.getName() << ", Color: " << banana.getColor() << ", Weight: " << banana.getWeight() << " kg" << endl;
cout << "Fruit: " << mango.getName() << ", Color: " << mango.getColor() << ", Weight: " << mango.getWeight() << " kg" << endl;
return 0;
}
The qeustion is, my lecturer ask me to use try and catch exception... how to add the try and catch ???? Im new in C++, 30/3 is my due date for submit this task. :'( anyone please help... thanks alot :) CHeers!
Thanks.
Last edited on Mar 28, 2012 at 2:31am UTC
Mar 28, 2012 at 3:29am UTC
pls help.....pls..
Mar 28, 2012 at 3:42am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main(void )
{
Banana banana;
Mango mango;
try {
cout << "Fruit: " << banana.getName() << ", Color: " << banana.getColor() << ", Weight: " << banana.getWeight() << " kg" << endl;
cout << "Fruit: " << mango.getName() << ", Color: " << mango.getColor() << ", Weight: " << mango.getWeight() << " kg" << endl;
}
catch (exception ex)
{
throw ex;
}
return 0;
}
Basically look like this, should research on the web more....
Last edited on Mar 28, 2012 at 3:46am UTC
Topic archived. No new replies allowed.