Problem 1 Define a base class named Pet. It has members (choose good data types
ID: 3567719 • Letter: P
Question
Problem 1
Define a base class named Pet. It has members (choose good data types for these)
1. Name
2. Gender (ie male/female)
3. Age
4. Weight
Add constructors, destructors, get/set methods, atleast one of each.
Define a function getDetail() that returns a int with value 0.
Define a Cat derived from Pet. This class has two private members
1. Breed
2. Type of Cat (three known types ie domestic, wild, toy)
Write constructor, destructor, get/set methods.
Override (ie re-define) getDetail() that returns the age of the Cat.
Problem 2
1. Write a program to print the values of diagonal elements of a
8x8 square matrix. Diagonal matrix is set as 8 x 8 (ie 8 rows 8 columns)
(so you can hard code the dimension)
Diagonal elements of a matrix are at index pairs (as I define) are 0,0 1,1 2,2 3,3 etc. up and
including the last indexed element.
Explanation / Answer
A1)
#include <iostream>
#include <string>
using namespace std;
class pet{
private:
string name;
string gender;
int age;
int weight;
public:
pet();
~pet();
void setname();
void setgender();
string getname();
string getgender();
void setage();
int getage();
void setweight();
int getweight();
int getdetails();
};
pet::pet(){
setname();
setgender();
setage();
setweight();
}
pet::~pet(){
// delete object
}
void pet::setname(){
cout << "Enter name: ";
cin >> name;
}
void pet::setgender(){
cout << "Enter Gender: ";
cin >> gender;
}
void pet::setage(){
cout << "Enter age: ";
cin >> age;
}
void pet::setweight(){
cout << "Enter weight: ";
cin >> weight;
}
string pet::getname(){
return name;
}
string pet::getgender(){
return gender;
}
int pet::getweight(){
return weight;
}
int pet::getage(){
return age;
}
int pet::getdetails(){
return 0;
}
class cat :public pet{
private:
string breed;
string type;
public:
cat();
~cat();
int getdetails();
};
cat::cat(){
}
cat::~cat(){
// delete cat object
}
int cat::getdetails(){
pet::getdetails();
return getage();
}
int main(){
cout << "Enter details of pet" << endl;
pet p = pet();
cout << "Enter details of cat" << endl;
cat c = cat();
cout << "getdetails of pet returns " << p.getdetails() << endl;
cout << "getdetails of cat returns " << c.getdetails() << endl;
return 0;
}
A2)
#include <iostream>
#include <string>
#include <random>
using namespace std;
int main(){
int cols = 8;
int rows = 8;
int **arr = new int*[rows];
cout << "Matrix is: " << endl;
for (int i = 0; i < rows; ++i){
int *temp = new int[cols];
for (int j = 0; j < cols; ++j){
temp[j] = rand() % 10;
cout << temp[j] << " ";
}
arr[i] = temp;
cout << endl;
}
cout << endl;
cout << "Diagonal elements are: ";
for (int i = 0; i < rows; ++i){
for (int j = 0; j < cols; ++j){
if (i == j){
cout << arr[i][j] << ", ";
}
}
}
cout << endl;
return 0;
}
links to the files are in the comments below
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.