When we are using a "double" pointer to class, what are we writing in the constructor with arguments? Are we using one pointer for the allocated memory ?
Here is the code. It doesn't compile and I don't understand why. Thanks for the help.
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
class Article{
private:
char title[100];
char author[50];
char *content;
bool publish;
public:
Article(char *title="", char *author="", char *content="", bool publish=0){
strcpy(this->title, title);
strcpy(this->author, author);
this->content=new char[strlen(content)+1];
strcpy(this->content, content);
this->publish=publish;
}
void show(){
cout<<title<<endl;
cout<<author<<endl;
cout<<content<endl;
}
~Article(){
delete [] content;
}
};
class Newspaper{
private:
char name[100];
Article **p;
int articles;
Article first;
public:
Newspaper(char *name="", Article **p=Article(), int articles=0, Article first=Article()){
strcpy(this->name, name);
}
};
int main() {
char title[100], author[50], content[100];
int n;
cin >> n;
char name[100];
cin.getline(name, 100);
cin.getline(name, 100);
Article first("VAZNO","OOP","Vezba:OOP",true);
Newspaper v(name,first);
Article **s = new Article*[n];
for(int i = 0; i < n; ++i) {
cin.getline(title, 100);
cin.getline(author, 50);
cin.getline(content, 100);
v.addArticle(Article(title, author, content, true)); //se koristi copy konstruktor
}
v.showFirst();
v.showLongest();
cout << v.totalofauthor(author) << endl;
for(int i = 0; i < n; ++i) {
delete s[i];
}
delete [] s;
return 0;
}
Solution to your question:
From your code it seems that a Newspaper uses a pointer to a pointer (what you call a double pointer) for keeping track of the Articles:
Newspaper(char *name, Article **p , // This would be ok
int articles = 0, Article first = Article())
but that you've trouble to define a default argument for it:
Newspaper(char *name="", Article **p=Article(), // oops doesn't compile
int articles = 0, Article first = Article())
The reason is that a "double" pointer is not the same as the object itself. If you want a default argument here, you must provide a double pointer as well.
This code does exactly this:
Newspaper(char *name = "", Article **p = new Article*, // this is ok !
int articles = 0, Article first = Article()){
So this is the solution to your question.
But what's your problem ?
But what's the purpose or providing a dummy pointer, pointing to nowhere as default argument ?
Later you try to create a Newspaper based on an article again:
Newspaper v(name, first); // won't work, because first is not a pointer either
So the problem is not the constructor, but the whole principle. It seems that you really want to create a Newspaper based on an Article, and that you use defaults to cover the case where someone wants to create a Newspaper without Article.
Apparently your design also foresees that Articles may be added dynamically:
v.addArticle(Article(title, author, content, true));
Finally it seems that you really have a problem with pointers: when in the constructor you write, you didn't initialise the poitner to a memory region large enough to hold the name:
Newspaper(char *name = "", ...) {
strcpy(this->name, name); // Ouch !! pointer name is not initalized !
}
So once you'll get your code compiled, your programme won't work ! As this->name is a pointner that is never initialized, your strcpy() will cause memory corruption and undefined behaviour (crash!?).
Recommendations
First, get a book or a tutorial to understand and master pointers. If you do'nt you'll quickly be completely lost in your C++ class.
In the meantime, get rid of your char* and strcpy() and alike, and use std::string instead.
Last, consider the use of std::vector to manage the dynamic Article container. A dynamic array of poitners implemented with Article** would need extra logic, such as maintaining the size, reallocation of memory once the number of article grows, not speaking of the ownership (allocation/deallocation) of the Articles you put in the array.
Based on these recommendations, your Newspaper would look like this:
class Newspaper{
private:
string name;
vector<Article> p; // p.size() will provide the number of articles
public:
Newspaper(string name = "") : name(name) { } // empty newspaper
Newspaper(string name, Article& a) : Newspaper(name) { // combo
addArticle(a);
}
void addArticle(Article &a) {
p.push_back(a); // add and article to the vector
}
... // rest of the code, here
};
And here a little more
Related
I am learning to use void pointers. Here I have created a resource class which can store any type of structure in (void *) and now when I try to get that structure back in (void *), I am not able to get same data back. Please help here
#include <iostream>
using namespace std;
class resource{
private:
void* data;
public:
void set(void* val){
data = val;
}
void get(void* val){
val = data;
}
};
class student{
struct st{
int id;
int age;
};
public:
void func(){
st *st1 = (st *)malloc(sizeof(st));
st1->id = 5;
st1->age = 10;
resource *rsrc = new resource();
rsrc->set((void*)st1);
void *data;
rsrc->get(&data);
st *st2 = (st*)data;
cout<<"get data %d"<<st2->id<<endl; // HERE I want to get same data back
}
};
int main() {
student *stu = new student();
stu->func();
return 0;
}
Just change the signature of get to return a void *:
void *get(){
return data;
}
And subsequently:
void *data = rsrc->get();
Also, it is idiomatic to use new, rather than malloc, to construct objects, although for POD (plain-old-data) types, either is valid.
Your get method won't return any value. You are passing a void pointer to it and inside the method, you overwrite that void pointer. However, that only overwrites the local copy of the void pointer and does not return a value. As others stated, you either have to pass a pointer to a pointer or use the return statement to return the value of the pointer.
In your specific case, #PaulSanders made the correct suggestion. You should use the getter/setter pattern. Also, he is correct that you should use new and delete in idomatic C++.
For user-created classes, most people specify classes with the first letter as a captial:
class Resource {};
class Student {};
It depends on your coding standard. It matters less whether you use camel case or not so long as you use a consistent naming convention.
Also, we generally try to prevent loss of type information in C++. Of course, you can use a C-style cast which will just reinterpret the pointer as the specified type but that is considered bad style and likely to cause problems.
Multiple students can associate with a single Department and single student can
associate with multiple Departments, but there is no ownership between the objects
and both have their own lifecycle. Both can create and delete independently.
WAP in C++ to model the relationships.
I have implemented this code as follows
#include<iostream>
#include<cstring>
using namespace std;
class Student
{
char* name_p;
public:
Student(char *sName)
{
cout<<"Student constructor called\n";
name_p=new char(sizeof(strlen(sName)));
name_p=sName;
}
~Student()
{
cout<<"Student destructor called\n";
delete name_p;
};
char* sName()
{
return name_p;
}
};
class Department
{
char* name_p;
public:
Department(char *dName)
{
cout<<"Department destructor called\n";
name_p=new char(sizeof(strlen(dName)));
name_p=dName;
}
~Department()
{
cout<<"Department destructor called\n";
delete name_p;
}
char* dName()
{
return name_p;
}
};
class Course
{
Student* std_p;
Department* dept_p;
char* courseName_p;
static unsigned int index;
static Course *courseList_p[4];
public:
Course(char* crseName,Student* student,Department* dept)
{
cout<<"Course constructor called\n";
std_p=student;
dept_p=dept;
if(index<4)
{
courseName_p=new char(sizeof(strlen(crseName)));
courseName_p=crseName;
courseList_p[index]=this;
++index;
}
else
{
cout<<"Cannot accomodate any more Course\n";
}
};
~Course()
{
cout<<"Course destructor called\n";
delete courseName_p;
};
static char* findStudent(char *crseName, char* deptName)
{
for(int i=0; i<index; i++)
{
if ( (courseList_p[i]->getCourseName() == crseName) &&
(courseList_p[i]->getDeptName() == deptName) )
{
return(courseList_p[i]->getStdName());
}
}
}
char* getStdName()
{
return std_p->sName();
};
char* getDeptName()
{
return dept_p->dName();
};
char* getCourseName()
{
return courseName_p;
};
};
unsigned int Course::index =0;
Course* Course::courseList_p[4]={0,0,0,0};
int main()
{
int i;
cout<<"\nExample of Association class\n";
cout<<"-----------------------------------\n\n";
cout<<"We have got 4 students\n";
Student *studentNames[4] = {new Student("Meera"), new Student("Moina"), new Student("Teena"), new Student("Mridula")} ;
cout<<"\n";
cout<<"We have got 2 Departments\n";
Department *departNames[2] = {new Department("Mathematics"), new Department("ComputerSceince")} ;
cout<<"\n";
cout<<"Here class Course Associates Student and Department, with a Course name\n";
Course course1("DataStructure",studentNames[0], departNames[1]);
Course course2("Maths",studentNames[3], departNames[0]);
Course course3("Geometry",studentNames[2], departNames[0]);
Course course4("CA",studentNames[1], departNames[1]);
cout<<"\n";
cout<<"Finding a Student using Course and Department\n";
cout<<"Student who has taken Maths Course in Mathematics Department is:"<<Course::findStudent("Maths", "Mathematics")<<endl;
cout<<"\n";
cout<<"Deletion of objects\n\n";
for(i=0;i<4;i++)
{
delete studentNames[i];
}
cout<<"\n";
for(i=0;i<2;i++)
{
delete departNames[i];
}
cout<<"\n";
return 0;
}
The code is showing warnings in main function i.e. ISO C++ forbids converting a string constant to char* and also the main() function is not returning 0 but a garbage value. Please help me in rectify these errors and warnings.
Also I don't want to use this pointer in class course, can I implement the code without using this pointer.
There are a couple of problems with your code. Mostly they are related to the use of char* instead of string. I'm amazed to see that some teacher still are found of this practice.
c-string allocation problems
The first problem causes memory corruption. This could explain that your main() returns garbage instead of 0. For example:
name_p=new char(sizeof(strlen(dName))); // OUCH!!!
In the statement you allocate it wrong. If you want to allocate an array, you must use new[...]. Moreover, keep in mind that a c-string allways needs 1 char more for the trailing null. So it should be:
name_p=new char[strlen(dName)+1];
would already improve the situation.
Last but not the least, if you allocate an array you must delete an array with delete[]
c-string copying issue
Then, you can copy C++ string with an assignment operator just as in:
name_p=sName; // OUCH
However, for char*, this just overwrites the pointer that new returned and you are not sure that the c-string it points to will still be valid when it's used.
If you want to assign a c-string, you need to go for:
strcpy (name_p, sName); // C-style
copy (sName, sName+strlen(sName)+2, name_p); // Safer C++ alternative
c-string comparison issue
Suppose you have two pointers to c string, say pa and pb. Comparing the pointers looks at the memory address, not at the content. So yu could have two identical strings but the comparison could nevertheless fail. THis kind of comparison would work perfectly well for c++ string, but for cstring, you need to to
strcmp(pa, pb)==0 // instead of pa == pb
c-strings in classes and structures
Whenever you have some pointers that you allocate in a class, you need to think of the rule of 3. Meaning that you'd need to add a copy constructor and an assignment operator. Fortunately, at first sight it seems that you don't need them here. Of couse, if you'd use c++ string you wouldn't have to worry about such details.
c-string conversion issues?
ANother issue in your code is the use of string literals with constructors. For example:
new Student("Meera").... // c-string literals (constant)
Student(char*...) // constructor using a c-string pointer (non constant)
You can easily get rid of this complaint, by making the constructor's pointer argument as constant:
Student(const char *sName)...
The same applies to function arguments, such as in find.
Other problems
The static member findStudent() works well, but only if a result is found. If nothing is found, it's UB, since there is no return statement (put a return nullptr; at the end of this function?)
Moreover, the wya you use this function, directly printing its result is dangerous, because dereferencing an invalid pointer will lead to UB.
Here the corrected code. It compiles and seem to run. I didn't analyse for more issues.
My experience so far is mostly in Java but i'm trying to learn C++ and i'm still struggling to understand pointers, overloads etc. Forgive me about the title but i didn't know how to present my problem. Anyway i came across this exercise while studying and i would like some help. The exercise provides a class Container and a main() and i'm supposed to extend the Class and make the main() work to provide the proper result.
template <typename T>
class Container
{
protected:
T * storage;
size_t num_items;
size_t storage_size;
public:
virtual void operator += (const T item) { }
virtual ~Container() { }
Container() : storage(nullptr), num_items(0), storage_size(0) { }
T operator [] (size_t index)
{
if (num_items==0)
return T(0);
return storage[index<num_items?index:num_items];
}
inline size_t size() { return num_items; }
};
void main(int argc, char* argv[])
{
Container<long> * store = new Container_ex(); // I'm suppose to fill this gap
---------------
size_t num_data;
cin >> num_data;
for (size_t i=0; i<num_data; i++)
{
long item;
cin >> item;
*store+=item;
}
for (size_t i=0; i<store->size(); i++)
std::cout << (*store)[i] << " ";
std::cout << std::endl;
delete store;
}
So far i've done this. The problem lies when i try to overload += cause i get an exception error (this->storage was nullptr) but in any case i don't understand the use of storage completely in this exercise so i would appreciate any help.
class Container_ex : public Container<long> {
public:
Container_ex() : Container() {}
~Container_ex() { delete[] storage; }
void operator += (const long item)
{
*storage = const_cast<long&>(item);
*storage = *storage + item;
num_items = num_items + 1;
storage_size = storage_size + 1;
}
};
Your pointer needs to point on a valid memory address before you can do anything with it. If it doesn't, it leads to undefined behavior that can end in a crash of your application, for example.
But when do you need to do it ? Certainly not in the operator overload. When you create an object, you must ensure that it is in a valid state. It means either setting it to nullptr so it is a null pointer, or allocating memory for it.
If you do it in operator overload, how can you be sure that operator overloading will be used ? Maybe the user just wants doing []. So you have to make a constructor allocating memory for your pointer. Or, better, use smart pointers that will save your life and provent headaches, especially if you come from a language where there is no explicit pointer like Java.
Finally, there are some great books to learn C++ better than the course you use.
Programming: Principles and Practice Using C++ by Bjarne Stroustrup, the creator of C++. Assume no previous experience in programming. Check to take the updated version for C++11 | C++14.
C++ Primer by Stanley Lippman, Josée Lajoie and Barbara E. Moo. ~1k pages, full of details and explanations on C++11. Good for beginners with a previous experience in programming.
I am new to C++ and there is a basic problem I am dealing with. The code below gives complier error:
#include <iostream>
using namespace std;
struct contact_info {
long number;
string name;
};
contact_info take(){
contact_info takein[2];
for (int i=0; i<2; i++) {
cout<<"what is the name"<<"\n";
getline(cin,takein[i].name);
cout<<"what is the phone number"<<"\n";
cin>>takein[i].number;
};
return takein;
};
void give(contact_info takein){
cout<<"Name:"<<takein.name<<"\n"<<"Number:"<<takein.number;
};
int main(int argc, const char * argv[])
{
contact_info takein;
takein=take();
give(takein);
return 0;
}
The error comes from function "take" and is "No viable conversion from 'contact_info[2]' to 'contact_info'"
The code is supposed to take two contact informations in a loop and then prints them on the screen.
I think I need to use pointers for that to pass the "takein" from "take" function to "main" function. Can anyone says if I can fix the code using array and not pointer?
#include <iostream>
struct contact_info
{
long number;
string name;
};
void take(contact_info takein[2])
{
for (int i=0; i<2; i++)
{
cout<<"what is the name"<<"\n";
getline(cin,takein[i].name);
cout<<"what is the phone number"<<"\n";
cin>>takein[i].number;
};
};
void give(contact_info takein)
{
cout<<"Name:"<<takein.name<<"\n"<<"Number:"<<takein.number;
};
int main()
{
contact_info takein[2];
take(takein);
for(int i=0;i<2;++i)
give(takein[i]);
return 0;
}
return takein;
but
contact_info takein[2];
Compiler is upset because you're trying to return an array, use return takin[0]; or return takin[1]; to return a specific contact_info
Addendum
Don't be afraid to learn Python first, higher level languages are a lot more forgiving and python is by no means a toy language, Python and C++ are two things I use daily and I love them both. I tolerate Java, it can be good when you want static typing and something more forgiving than C++, or in my case Android :P
If you edit your question to describe what you want to do I am happy to provide some annotated code to demonstrate, leave a comment to this to get my attention.
contact_info[] take(){
...
}
if you are trying to return an array.
for returning an element, use
return takein[0];
or
return takein[1];
If your goal is "to take two contact informations in a loop and then prints them on the screen", you must pass an array to get information and pass an array to prints it:
contact_info * take(contact_info *takein){
for (int i=0; i<2; i++) {
cout<<"what is the name"<<"\n";
getline(cin, takein[i].name);
cout<<"what is the phone number"<<"\n";
cin>>takein[i].number;
};
return takein;
};
void give(contact_info * takein){
for (int i = 0; i < 2; i++)
cout<<"Name:"<<takein[i].name<<"\n"<<"Number:"<<takein[i].number;
};
int main(int argc, const char * argv[])
{
contact_info takein[2];
// pass takein as an array
take(takein);
give(takein);
return 0;
}
Secret's out! Arrays are pointers. Or at least, they implicitly convert to them. Even it you did have the right type, you'd be using undefined behavior, as the array is allocated on a place on the stack that no longer exists.
I'll assume you actually want to 'return two things', otherwise Alec's answer is perfectly valid.
Try a vector, or a 2 element struct. People often struggle mentally with the notion of creating a struct only to serve as a return value, but it's worth it from a maintainability standpoint. You get the power of naming the type and naming its contents, and that self-documents your intent.
In the main function takein is a variable of type contact_info
and here takein=take(); you are trying assign an array to a normal variable.
This if for my homework.
I have a class called Student that takes 3 parameters (id, name, class) and I want to store each student in an array called Roster (which can only have 7 students).
The user will provides input to add or remove students. Thus, I have to manage the array by creating or deleting students. So if the user specify the student ID, I have to remove him for the array.
I tried to use a fixed array, but I'm struggling to make it works. Is there a better way to implement this?
I must not use a vector or any STL container.
student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
static const int SIZE = 7;
class Student {
private:
int student_id;
std::string name;
std::string classification;
public:
Student(int, std::string, std::string); // constructor; initialize the list to be empty
~Student();
void print();
};
#endif
student.cpp
#include <iostream>
#include <string>
#include "student.h"
#define PROMPT "class> "
using namespace std;
Student::Student(int a, string b, string c){
student_id = a;
name = b;
classification = c;
}
Student::~Student(){
//delete Student
}
void Student::print(){
cout<<"Enrolled:"<<endl;
cout<<student_id<<"-"<<name<<"-"<<classification<<endl;
}
main.cpp
#include <iostream>
#include <string>
//#include <sstream>
#include "student.h"
#define PROMPT "class> "
using namespace std;
//**** Implement Error Handling ****\\
enum errorType {
UNKNOWN_ERROR,
INPUT_ERROR,
HANDLER,
NUM_ERRORS
};
// error messages
string errorMessage[NUM_ERRORS] = {
"Unknown Error\n",
"Input Error\n",
};
// error handler
void handleError(errorType err) {
if(err > 0 && err < NUM_ERRORS)
cout<< "Error: "<< errorMessage[err];
else cout<< "Error: "<< errorMessage[UNKNOWN_ERROR];
}
//**** END Error Handling ****\\
void enroll(Student newStudent){
cout<<"test";
Student roster[SIZE];
for(int i=0;i<SIZE;i++){
newStudent->roster[i];
}
}
void handleInput() {
int id; string n, c;
cin>>id>>n>>c;
Student newStudent(id,n,c);
newStudent.print();
enroll(newStudent);
//cout<<"hello3"<<endl;
return;
}
int main() {
//Student newStudent; /* <-- why doesn't this work?!*/
string input = "";
bool finished = false;
cout<<PROMPT; // prompt the user
while(!finished) {
if(input!="") cout<<PROMPT;
cin>>input;
if(input=="enroll") {
cout<<PROMPT<<"Enroll student:"<<endl;
handleInput();
}
else if(input=="drop") {
cout<<PROMPT<<"Enter ID:"<<endl;
}
else if(input=="roster") {
cout<<"This will print formatted list of students"<<endl;
}
else if(input=="quit") {
finished=true;
}
else handleError(errorType(1));
}
}
Since it is a homework, I'd like to point out some mistakes you did because it is important to understand what you are doing in the first place.
You must not program by coincidence, but by trying to understand exactly what's going on. By doing that you will become better and better and the answers should fall in place.
What you've done
So, from what you are describing, the array is fixed. Thus it is a good idea to use a constant as you did (SIZE).
However, as we can see below you a declaring an array of size SIZE in the function. By doing that, your array is like a temporary variable, because its scope is inside the function. Each time you call this function, the array will be declared again and then deleted at the exit. So it should be declared outside.
void enroll(Student newStudent)
{
cout<<"test";
Student roster[SIZE]; // Here 'roster' will be available only inside the function.
for(int i=0;i<SIZE;i++)
{
newStudent->roster[i]; // Here there is few mistakes see my explanation below*
}
}
If we look at this part:
newStudent->roster[i];
First of all, the arrow '->' is used with pointers. The dot '.' is used with objects. In both case, it does the same thing, access to public members of Student.
Since you passed
void enroll(Student newStudent)
you should use '.' instead.
newStudent.SomeOfYourMembers;
If the parameter was a pointer to a Student
void enroll(Student *newStudent)
Then, you'd have to use the arrow '->' like you did.
Back to the original statement:
newStudent->roster[i];
This means, you want to access to 'roster' array at position 'i' inside your Student object (newStudent). As you can see in your code, roster is not declared inside Student (and should not be since you want an array of Students), so that won't work.
Guidelines
As I mentionned, your array should be outside the function, so at a higher scope.
Then, if you need an array of student, basically, 'roster[i]' will give you access to the student 'i'. Thus, if you want to print the student, you would do something like that:
roster[i].print();
This would be valid because 'print()' is defined as public.
In order to store a student inside the array, you can do something like:
roster[i] = new Student(0 /* id*/, "name", "classification");
But don't forget, each time you use new, you have to balance it with a delete. And if you are creating the student like this in a loop, you will have to clean them the same way:
for(int i = 0; i < SIZE; ++i)
{
delete roster[i];
}
Good luck!
Don't hesitate if there is there anything that I could clarify. I hope this helps!
Edit: In reply to your first comment.
Concerning the roster array
No, it is not mandatory to create a class roster you could declare roster in the main.cpp.
The key concept is that by defining
Student roster[SIZE];
the array will contains objects of type Student.
What roster[i].print() means is that you are printing one of the Student of that array, in fact the one at position 'i'.
Concerning the print() function
What is powerfull with Object Oriented language, each object will have the same print() function. So, you do not need to convert the array to string.
However, if you want a string to be printed out (or returned) you can write the code inside the print() function that will do this job.
The advantage of this, is that if further on you need to change your array in some ways, your print() function will always work.
Concerning the Delete
When you are doing something like this on an array that contains objects:
delete roster[i];
It will delete the object at the position 'i'. Thus, the destructor of that Student 'i' will be called. If your object Student would contains other object, you would have to delete them in the destructor.
Further notices
Since ID is an input that you are storing into a string, you will have to convert the ID to the same type of the student_id, which is a int. Then you can always write a loop for each student and check their ID to delete the proper one.
Concerning the container, a fixed array might not be the best to achieve this job. You might want to look the LinkedList concept.
It doesn't make much sense for enroll to be a member function, so
I'd wrap the roster into a class to get automatic clean up of my
pointers.
#include <cstddef>
struct Student {};
class Roster
{
private:
static const size_t size = 7;
// non-copyable
Roster(const Roster&);
Roster& operator=(const Roster&);
public:
Roster() {
for(unsigned i = 0; i < size; ++i) {
roster_[i] = NULL;
}
}
~Roster() {
for(unsigned i = 0; i < size; ++i) {
delete roster_[i];
}
}
// enroll by copy
bool enroll(const Student& s) {
for(unsigned i = 0; i < size; ++i) {
if(roster_[i] == NULL) {
roster_[i] = new Student(s);
return true;
}
}
// out of space
return false;
}
// enroll by taking ownership
bool enroll(Student* s) {
for(unsigned i = 0; i < size; ++i) {
if(roster_[i] == NULL) {
roster_[i] = s;
return true;
}
}
// out of space
return false;
}
private:
// data
Student* roster_[size];
};
int main()
{
Roster r;
Student s;
r.enroll(s);
Student* sp = new Student();
r.enroll(sp);
return 0;
}
What about this?
Student * roster[2];
roster[0] = new Student(5,"first","2A");
roster[1] = new Student(2,"Second","5B");
Ps:
Enrol and Size shouldn't be members of the student class.
Print should ideally be externalized and a ToString function should be added instead.
You should use the inline constructor initialization instead:
Student(int a,string b,string c):id(a),name(b),class(c){}
You've used the keyword class as a variable name of type string. You shouldn't do that. Does it even compile like that?
enroll should have two arguments: void enroll( Student enrollee, Student Roster[]). You should probably change the name of Roster to roster because it's not a class and typically class names are capitalized.
If your array will only ever have 7 students then you could use some sentinel value to mark that the current student as an invalid student. Perhaps the id will be -1 to mark this. It means basically that you need some way to keep track of which spots in the array you can still use. If you don't do this then declaring an array of Students will get you an array of students with garbage member variables. You wouldn't be able to tell which students are real ones and which are just place holders for when someone new enrolls in the class. I would create a default constructor of Student and initialize its member variables like this:
id=-1;
name="";
name_of_class="";
I changed the name of your string class to avoid confusion.
After all that, enroll would look something like this:
void Student::enroll( Student enrolee, Student roster[]){
//search through roster to check for the first student with an
//id of -1
//if there are no students with id of -1, produce an error message
//that the class is full
//overwrite the student with id of -1 with the id, name, and
//name_of_class of enrollee
}
Although I'm not sure what exactly string class is there for. Does it store what class the Student is in? Is it their year in school like Freshman, Sophomore?
If you're suppose to use dynamic allocation of roster, though, it's a different story, but you said it will only ever have seven students.