I have a few questions that I couldn't figure out and was wondering if anyone could help me so that I have something accurate to study on for my next exam. Even if you can send me a link to somewhere else where I can read about something, it would help a ton!
Write the class declaration of array that has, as private data, a dynamic array named buckets and an integer named size. It also has, as public data, a default constructor and a set function that returns nothing and takes an index and value as parameters. Write the set function for the question as well.
When overloading <<, what sort of function needs to be declared?
What three keywords are associated with exception handling?
When is exception handling used?
Write a + operator overload for a fraction class (In other words, write a function that will let you add things like 1/2 + 2/2 and get a value of 3/2.
What are conveniences like operator and function overloading called?
Write the class declaration of array that has, as private data, a dynamic array named buckets and an integer named size. It also has, as public data, a default constructor and a set function that returns nothing and takes an index and value as parameters. Write the set function for the question as well.
You have a class with a bit data and the function, that should be easy.
1 2 3 4 5 6 7 8 9 10 11
template <typename T>
class Array
{
public:
void set(std::size_t position, T value);
// Array() is a compiler generated function if no other constructor is declared
private:
T* buckets;
int size;
};
When overloading <<, what sort of function needs to be declared?
This question asks what needs to be done that the operator can access your private data.
The keyword to accomplish this is using the keyword friend
1 2 3 4 5 6 7 8 9 10 11 12 13 14
template <typename T>
class Array
{
public:
void set(std::size_t position, T value) { ... }
// Array() is a compiler generated function if no other constructor is declared
private:
T* buckets;
int size;
friend std::ostream& operator<<(std::ostream&, const Array&);
};
std::ostream& operator<<(std::ostream& os, const Array& arr) { ... }
What three keywords are associated with exception handling?
try, catch and exception?
When is exception handling used?
To handle problems that occur during run-time?
Write a + operator overload for a fraction class (In other words, write a function that will let you add things like 1/2 + 2/2 and get a value of 3/2.
1 2 3 4 5 6 7 8
template <typename T>
class fraction
{
T numinorator;
T dominator;
public:
fraction& operator+(fraction rhs) { ... }
};
You might have come this far but the implementation is up to you
What are conveniences like operator and function overloading called?
Ad hoc polymorphism
Main article: Ad hoc polymorphism
Chris Strachey[4] chose the term ad hoc polymorphism to refer to polymorphic functions which can be applied to arguments of different types, but which behave differently depending on the type of the argument to which they are applied (also known as function overloading or operator overloading).