Question

1. Questions on inheritance. (a) Consider the following program segment: 1 class Array2 2 private: ClassX a: // ClassY b: //
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
//declared inorder to run the code
class ClassX
{
  
};
class ClassY
{
  
};
class Array2
{
   private:
       ClassX *a;
       ClassY *b;
   public:
       Array2(int size)//constructor
       {
           a = new ClassX[size];
           b = new ClassY[size];  
       }
       //getters
       ClassX *getA()
       {
           return a;
           }
       ClassY *getB()
       {
           return b;}
       //setters
       void setA(ClassX *a)
       {
           this->a = a;  
       }
       void setB(ClassY *b)
       {
           this->b = b;  
       }
       //destructor
       ~Array2()
       {
           delete a;
           delete b;  
       }
};
class Array3:public Array2//class Array3 inherits Array2
{
   //i
   private:
   int size;
   int currentx,currenty;
   //ii
   public:
   //constructor
   Array3(int s):Array2(s)//calling base class constructor
   {
       size=s;
       //to keep track of elements in array
       //work as index
       currentx=0;
       currenty=0;
   }
   //iii
   int getSize()
   {
       return size;//returning size
   }  
   //iv
   //overloading insert methods
   void insert(ClassX a)
   {
       if(currentx+1 <size)
       {
       ClassX *A = getA();//getting classX
       A[currentx+1]=a;//inserting to array
       currentx++;//
       }  
   }
   void insert(ClassY a)
   {
       if(currenty+1 <size)
       {
       ClassY *B = getB();//getting classY
       B[currenty+1]=a;//inserting to array
       currenty++;//
       }  
   }
   //v
   //destructor
   ~Array3()
   {
       delete getA();
       delete getB();  
   }
};
int main()
{
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
1. Questions on inheritance. (a) Consider the following program segment: 1 class Array2 2 private: ClassX...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given the following class: class Q2 { private int a; private int b; private int c;...

    Given the following class: class Q2 { private int a; private int b; private int c; public void setA(int a){this.a = a; } public void setB(int b){this.b = b;} public void setc(int c){this.c = c;} public int geta(){return a; } public int gets(){return b;} public int getc(){return c;} public int m1(int a, int b){ return a + b; public boolean m2 (int x, int y){ return m1(x, y) + x + y < 10; What is the output of the...

  • please help fill out the class menu create a prototype that will display information about housing...

    please help fill out the class menu create a prototype that will display information about housing data1. In this prototype you will use dynamic array data structures to store the data and compute metrics. For the requirements of the prototype, your client has given you a list of metrics and operations. The user interface will be a menu that displays this list and prompts the user to choose which option to execute.You will create two classes. First you will create...

  • Consider the following template class: template <typename T> class ArrayOfTen { public: ArrayOfTen(); ~ArrayOfTen(); void insert(T);...

    Consider the following template class: template <typename T> class ArrayOfTen { public: ArrayOfTen(); ~ArrayOfTen(); void insert(T); void printAll(); private: T * array; int size; } a. When the array is first created, it should allocate memory for 10 items. Size should be zero. Write the constructor: b. When ArrayOfTen::insert(T) is called, the new item should be added to the array, and size should increase by one. If the array already has 10 items, then it should throw an exception. Write...

  • I’m giving you code for a Class called GenericArray, which is an array that takes a...

    I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...

  • Question 1 10 pts Inheritance is a capability of C++ to represent increased specialization that is...

    Question 1 10 pts Inheritance is a capability of C++ to represent increased specialization that is often found in real world relationships. True False Question 2 10 pts Inheritance between a child and parent object exhibits an 'relationship. Question 3 10 pts As you move down an inheritance relationship from parent to child, you typically move from a general to specific description. True False Question 4 10 pts The syntax for declaring DerivedClass to publicly inherit from BaseClass is given...

  • This program has an array of floating point numbers as a private data member of a...

    This program has an array of floating point numbers as a private data member of a class. The data file contains floating point temperatures which are read by a member function of the class and stored in the array. Exercise 1: Why does the member function printList have a const after its name but getList does not? Exercise 2: Fill in the code so that the program reads in the data values from the temperature file and prints them to...

  • Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the App...

    Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the Appendix For this question, a. (2 marks Consider the following heap 30 12 20 19 6 10 18 Given the array representation of a heap discussed in class, what is the array that corre sponds to this heap? b. (5 marks) Successively insert into the heap of part (a.) 22, 35 and 11, in that order. Use the standard heap insertion algorithm....

  • C++ Question Modify the class by changing private members to protected members and viewing Account class...

    C++ Question Modify the class by changing private members to protected members and viewing Account class as a base class. Define two derived classes Checking and RRSP. The Checking class has one additional private data: total(double). It has five public member functions: a constructor Checking (string n, string addr, double b, int p, double tot) (note you must call base constructor by using initialization list), double getTotal() to return total, void setTotal() to updata data, void withdraw(double w) which requires...

  • I am struggling with a program in C++. it involves using a struct and a class...

    I am struggling with a program in C++. it involves using a struct and a class to create meal arrays from a user. I've written my main okay. but the functions in the class are giving me issues with constructors deconstructors. Instructions A restaurant in the Milky way galaxy called “Green Alien” has several meals available on their electronic menu. For each food item in each meal, the menu lists the calories per gram and the number of grams per...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT