#include #include #include #include #include class CAT{ public: float weight() const{return weight_;} unsigned int range() const{return range_;} CAT(float weight= 1.0, unsigned int range=1); int set_weight(float); int set_range(unsigned int power); int dig(double hours); int id() const{return id_;} private: float weight_; unsigned int range_; const int id_; }; int compare(CAT,CAT); Task: Define the parameterized constructor: CAT::CAT(float weight, unsigned int range): Provide a try-catch block as part of the constructor. Inside the try block, the function set_weight( ) is to be called with the argument weight. In the catch block, pass the exception message via << to std::cout Then weight_ is to be set in the following way: If range is smaller than 10, weight_ should be set to range In all other cases weight_ is to be set to weight_ times -1 After the try-catch block, proceed as follows: Assigned range to the member variable range_ . Additionally id_ is to be set to the value of calculated by adding range to weight.
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <exception>
class CAT{
public:
float weight() const{return weight_;}
unsigned int range() const{return range_;}
CAT(float weight= 1.0, unsigned int range=1);
int set_weight(float);
int set_range(unsigned int power);
int dig(double hours);
int id() const{return id_;}
private:
float weight_;
unsigned int range_;
const int id_;
};
int compare(CAT,CAT);
Task:
- If range is smaller than 10, weight_ should be set to range
- In all other cases weight_ is to be set to weight_ times -1
- Assigned range to the member variable range_ .
- Additionally id_ is to be set to the value of calculated by adding range to weight.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images