-7

So I have these classes:

In main I wrote an array of pointers:

student *arry[10];

How can I make each cell point to an object of a different class? For example : I want the cell 0 , 2 , 4 point to an object of class medstudent using ( new statement ) thank you

here is class medStudent

#include<iostream>
#include"student.cpp"
using namespace std;
class medStudent:public student {
  public :int clinicH;
public:
medStudent(int ch, string n , int i ):student(n,i){
  setClinicH(ch);
  cout << "New Medecine Student" << endl;
}

~medStudent(){
  cout << "Medecine Student Deleted" << endl;
}

medStudent(medStudent & ms):student(ms){
  cout << "New Copy Medecined Student" << endl;
}
  medstudent(){

  }
void setClinicH(int ch){
  clinicH = ch;
}

int getClinicH()const{
  return clinicH;
}

void print()const{
  student::print();
  cout << "Clinical Hours: " << getClinicH() << endl;
}
};

Here is class student:

#include <iostream>
//#include"medstudent.cpp"

using namespace std;
class student//:public medstudent
 {
public :
  static int numberOfSaeeds;
  const int id;
  string name;
public:

~student(){
    cout << "Delete Student: " << getName() << " " << endl ;
  }


student(string n, int i):id(i){
    setName(n);
    cout << "Student with args" << endl ;
  }

void setName(string n){
    name = n;
  }

string getName()const{
    return name;        
  }

void print()const{
    cout << "My name is: " << name << endl;
    cout << "My ID is: " << id << endl;
  }




void setNOS(int nos){
  numberOfSaeeds = nos;
}

int getNOS(){
  return numberOfSaeeds;
}

void printAddress()const{
  cout << "My address is " << this << endl;
}

student * getAddress(){
  return this;
}


student(student & sc):id(sc.id){
  name = sc.name;
  setName(sc.getName());
  cout << "New Object using the copy constructor" << endl;
}

};

Here is main code:

#include<iostream>
using  namespace std;
#include"time.cpp"
#include "student.cpp"
//#include"medstudent.cpp"

int main(){
        student a1("asa" , 2);
         student * a[10];
    a[3]= new student("jj", 22 );
    a[0] = new medStudent();
}
sebenalern
  • 2,515
  • 3
  • 26
  • 36
faris
  • 1
  • 1
  • 2
    `arry[0] = new MedStudent`? Something you should have learned by reading even the most basic beginners tutorial or book, which tells us that you don't really deserve our help. Unless you show some effort, why should we spend our effort and time? – Some programmer dude Mar 05 '16 at 18:52
  • do you think I am dumb and I didnt try this ? I have try this but it tells me that there is [Error] redefinition of 'class student' in student file – faris Mar 05 '16 at 19:59
  • If you did try this, why didn't you tell us you did? How about creating a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) of your attempt and show us? And then tell us how it did, or didn't work, what problems you have with it, what build errors you get, or when/where it crashes, or what output you expected and what you actually got. Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Mar 05 '16 at 20:01
  • I compiled all classes and there is no errors ! but in the main file it shows me that error after I write this line a[0] = new medstudent(); [Error] expected type-specifier before 'medstudent' this isn’t homework it is for my own knowledge – faris Mar 05 '16 at 20:10
  • Where's your code? Don't paste picture of code, but edit your question with the code. – Thomas Matthews Mar 05 '16 at 20:13
  • Thomas do you want all the cods or just the main ? – faris Mar 05 '16 at 20:15
  • Your image says `MedStudent` but you tell us `medstudent`. C++ is case sensitive. Really, please show us an [MCVE]((http://stackoverflow.com/help/mcve)), it really helps us understand what you're doing and what problems you might have. Remember the saying "a picture says more than a thousand words", well I have a new one: "Code says more than a thousand pictures". – Some programmer dude Mar 05 '16 at 20:19
  • I edited all the classes into the same spilling so this is not the problem Joachim sorry I don’t know how to add a picture in the comment ;( – faris Mar 05 '16 at 20:20
  • Don't add crucial information like code in a comment. Instead edit your question. And please oh please don't post images or pictures of code, copy-paste the actual text. – Some programmer dude Mar 05 '16 at 20:24
  • I hope you can read it now – faris Mar 05 '16 at 20:42

1 Answers1

0

Since you explicitly declare a medStudent constructor the compiler will not create a default constructor for your class. And when you do new medStudent(); you are (explicitly) trying to invoke the default constructor, which doesn't exist.

That will give you a build error, one that should have been very easy to diagnose if you read it and most importantly shown it to us (when asking questions about build errors, always include the complete and unedited error output, including any informational output, in the body of the question, together with the code causing the error).

The solution? Call the existing parameterized constructor. E.g. new medStudent("foo", 123).


By the way, if you want inheritance to work okay, and the base-class destructor to be called when deleting an object of a child-class, then you need to make the destructors virtual.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621