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;}