My code compiles and runs but does not do what I want.
It is NOT allowed to change main(). I'm trying to build Base(almost like how i typed), Derived classes and type their member functions.

//---------------------------------------------------------------------------
#include <vcl.h>
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
class Base{
public:
void Get_Array();
int max();
void print();
Base();
~Base();
Base(Base&);
int*& getpointer(void);
private:
int *p, size;
};
//----------------------------------------------------------------------------
class Derived:public Base{
public:
int *p, size;
Derived();
~Derived();
Derived(Derived& other);
void swap(Derived& other1, Derived& other2);
void swapsize(Derived& other2);
void Add(Derived& other1,Derived& other2);
Derived& operator-(Derived& other);
Derived& operator+=(Derived& other);
};

//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{       Derived A,B;
        A.Get_Array();  B.Get_Array();
        if(A.size==B.size){
                if(A.max()>B.max())
                swap(A,B);
                else A+=B;
                A.print(); }
                else{   Derived C(A);
                        C.size=A.size+B.size;
                        C.Add(A,B);
                    C.print(); }
        getch();
        return 0;
}
//---------------------------------------------------------------------------
void Base::Get_Array(){
cout<<"How many numbers are you going to enter: ";
   cin>>size;
   p=new int[size];
   cout<<"Enter your numbers: ";
   for(int i=0;i<size;i++)
   cin>>p[i];
   cout<<endl;}

int Base::max(){
int temp=p[0];
for(int i=0;i<size-1;i++)
if(p[i]>temp)
temp=p[i];
return temp;}

void Base::print(){
for(int i=0;i<size;i++)
cout<<p[i]<<" ";
cout<<endl; }

Base::Base(){
size=0;
p=NULL;}

Base::~Base(){
delete [] p;}

Base::Base(Base& other){
        size=other.size;
        p=new int[size];
        for(int i=0;i<size;i++)
        p[i]=other.p[i];}


int*& Base::getpointer(){
return p;}


Derived::Derived(){
size=0;
p=NULL;}

Derived::~Derived(){
delete[] p;}

Derived::Derived(Derived& other){
        size=other.size;
        p=new int[size];
        for(int i=0;i<size;i++)
        p[i]=other.p[i];}

void Derived::swap(Derived& other1, Derived& other2){
int *&q=other1.getpointer();
int *&k=other2.getpointer();
int * temp;
temp=q;
q=k;
k=temp;
other1.swapsize(other2);}

void Derived::swapsize(Derived& other2){
int temp;
temp=other2.size;
other2.size=size;
size=temp;}

void Derived::Add(Derived& other1,Derived& other2){
int *temp;
temp=new int[size+other1.size];
for(int i=0;i<other1.size;i++)
temp[i]=p[i];
for(int i=other1.size;i<size;i++)
temp[i]=other1.p[i-other1.size];
for(int i=size;i<size+other1.size;i++)
temp[i]=other2.p[i-size];
delete [] p;
p=temp;
size+=other1.size;}

Derived& Derived::operator-(Derived& other){
int *&q=getpointer();
int *&k=other.getpointer();
for(int i=0;i<size;i++)
q[i]-=k[i];
return *this;}

Derived& Derived::operator+=(Derived& other){
int *&q=getpointer();
int *&k=other.getpointer();
for(int i=0;i<size;i++)
q[i]+=k[i];
return *this;}

Recommended Answers

All 4 Replies

Could you cut the code down to 20 or 30 lines? The least amount of code which replicates the problem. Also could you use white space indentation? People tend to respond to posts which are easy on the eyes and short.

It's very hard to read your code since you have no indentation there and also we do not know even what your problem is.

How can you want us to fix your problem if you don't tell the problem ?

It's very hard to read your code since you have no indentation there and also we do not know even what your problem is.

How can you want us to fix your problem if you don't tell the problem ?

Look at only main() and then complete the code and make it executable. That's my target! There is no other original code except main() in my code. You don't need to be interested in other parts. It's like a puzzle. I give you main() and it's not allowed to change main(). By that way you can't use everything you want when you type code. So focus on main() and then type the rest which is required. If you have any questions about main(), ask it.
PS: EXTRA HELP- I think we need to build Base and Derived(public:Base) classes(has A,B and C objects). We must type constructor, destructor, copy constructor, overloading operator and the other member functions( which is required in main() ) of those classes.
PS: My code has no error. Because I modified it according to the errors. For example, using int *p, size; as public in Derived class sounds too stupid. However, I did it.
When I run the program, I enter the size and the numbers for the arrays in A and B objects. It always prints the first array in A object. It has to change according to the numbers I enter. You know there are some if-else sentences there.

A,B and C are the derived objects.
Get_Array():Gets the size and the numbers from the users. It creates dynamic arrays.
A.size,B.size:How many numbers are available in the array? Size is not a function.
max(): the greatest number in the array.
swap( A,B ): swaps the arrays in the objects of A and B.
print(): shows the numbers in the screen.
Add(): adds the numbers(from the arrays in the objects) at the end of the array

It is not allowed to change my main(). It's the keyword. I'm trying to type the rest of the code(classes and their member functions).

int main(int argc, char* argv[])  

{       Derived A,B;  

    A.Get_Array();  B.Get_Array();  

    if(A.size==B.size){  

        if(A.max()>B.max())  

            swap(A,B);  

        else A+=B;  

        A.print(); }  

    else{   Derived C(A);  

        C.size=A.size+B.size;  

        C.Add(A,B);  

        C.print(); }  

    getch();  

    return 0;  

}

My code is OK now...

//---------------------------------------------------------------------------
#include <vcl.h>
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
#pragma hdrstop
//---------------------------------------------------------------------------
class Base{
public:
void Get_Array();
int max();
void print();
Base();
~Base();
Base(Base&);
int*& getpointer(void);
int *p, size;
};
//----------------------------------------------------------------------------
class Derived:public Base{
public:
Derived();
~Derived();
Derived(Derived& other);
void swap(Derived& other1, Derived& other2);
void swapsize(Derived& other2);
void Add(Derived& other1,Derived& other2);
Derived& operator-(Derived& other);
Derived& operator+=(Derived& other);
};
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{       Derived A,B;
        A.Get_Array();  B.Get_Array();
        if(A.size==B.size){
                if(A.max()>B.max())
                swap(A,B);
                else A+=B;
                A.print(); }
                else{   Derived C(A);
                        C.size=A.size+B.size;
                        C.Add(A,B);
                        C.print(); }
        getch();
        return 0;
}
//---------------------------------------------------------------------------
void Base::Get_Array(){
cout<<"How many numbers are you going to enter: ";
   cin>>size;
   p=new int[size];
   cout<<"Enter your numbers: ";
   for(int i=0;i<size;i++)
   cin>>p[i];
   cout<<endl;}

int Base::max(){
int temp=p[0];
for(int i=0;i<size-1;i++)
if(p[i]>temp)
temp=p[i];
return temp;}

void Base::print(){
for(int i=0;i<size;i++)
cout<<p[i]<<" ";
cout<<endl; }

Base::Base(){
size=0;
p=NULL;}

Base::~Base(){
delete [] p;}

Base::Base(Base& other){
        size=other.size;
        p=new int[size];
        for(int i=0;i<size;i++)
        p[i]=other.p[i];}


int*& Base::getpointer(){
return p;}


Derived::Derived(){
size=0;
p=NULL;}

Derived::~Derived(){
}

Derived::Derived(Derived& other){
        size=other.size;
        p=new int[size];
        for(int i=0;i<size;i++)
        p[i]=other.p[i];}

void Derived::swap(Derived& other1, Derived& other2){
int *&q=other1.getpointer();
int *&k=other2.getpointer();
int * temp;
temp=q;
q=k;
k=temp;
other1.swapsize(other2);}

void Derived::swapsize(Derived& other2){
int temp;
temp=other2.size;
other2.size=size;
size=temp;}

void Derived::Add(Derived& other1,Derived& other2){
int *temp;
temp=new int[size+other1.size];
for(int i=0;i<other1.size;i++)
temp[i]=p[i];
for(int i=other1.size;i<2*other1.size;i++)
temp[i]=other1.p[i-other1.size];
for(int i=2*other1.size;i<size+other1.size;i++)
temp[i]=other2.p[i-2*other1.size];
delete [] p;
p=temp;
size+=other1.size;}

Derived& Derived::operator-(Derived& other){
int *&q=getpointer();
int *&k=other.getpointer();
for(int i=0;i<size;i++)
q[i]-=k[i];
return *this;}

Derived& Derived::operator+=(Derived& other){
int *&q=getpointer();
int *&k=other.getpointer();
for(int i=0;i<size;i++)
q[i]+=k[i];
return *this;}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.