I have made two classes Department and Student and then made third Class College in which i declared one object each of Department and Student class.
In the constructor of College class:
College(string clg_name, int num_dept, Department &dp, Student &s)
{
m_clgName=clg_name;
m_numDept=num_dept;
dep=dp;
st=s;
}
I am getting error:
error: No matching function for call to 'College::College(const char[5],int ,Department, student)
no known conversion for argument 3 'Department' from 'Department&'
-------------------------------------------------------------------------
Solution either: College(string clg_name, int num_dept, Department dp, Student s)
or College(string clg_name, int num_dept, const Department &dp, const Student &s)
so what difference does const make in constructor definition ?
---------------------------------------------------------------------------
Whole code is like this:
First I have a Student.h file and Department.h for student and department class
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
string m_stName;
int m_stID;
public:
Student(){}
Student(string name, int id){StdInfo(name,id);}
string GetStdName() {return m_stName;}
int GetStdID(){return m_stID;}
void StdInfo(string name, int id)
{
m_stName=name;
m_stID=id;
}
friend ostream& operator<<(ostream &os, const Student &st);
friend istream& operator>>(istream &is, Student &st);
};
ostream& operator<<(ostream &os, const Student &st)
{
os<<"\nStudent's name: "<<st.m_stName<<", ID: "<<st.m_stID<<endl;
return os;
}
istream& operator>>(istream &is, Student &st)
{
cout << "\nEnter student's name: ";
is >> st.m_stName;
cout << "\nEnter student's ID: ";
is >> st.m_stID;
return is;
}
#endif // STUDENT_H
#ifndef DEPARTMENT_H
#define DEPARTMENT_H
#include <iostream>
#include <cstring>
using namespace std;
class Department
{
string m_DeptName;
public:
Department(){}
Department(string name){ setDepName(name);}
void setDepName(string name){ m_DeptName=name;}
string getDeptName(){ return m_DeptName;}
friend ostream& operator<<(ostream &os, const Department &dp);
friend istream& operator>>(istream &is, Department &dp);
};
ostream& operator<<(ostream &os, const Department &dp)
{
os<<"Department's name: "<<dp.m_DeptName<<endl;
return os;
}
istream& operator>>(istream &is, Department &dp)
{
cout << "\nEnter Department name: ";
is >> dp.m_DeptName;
return is;
}
#endif // DEPARTMENT_H
finally main script:
#include <iostream>
#include <string>
#include <cstring>
#include "Student.h"
#include "Department.h"
using namespace std;
class College
{
string m_ClgName;
int m_numDept;
Department dep;
Student st;
College(){}
public:
College(string clg_name, int num_dept, Department &dp, Student &s)
{
m_ClgName=clg_name;
m_numDept=num_dept;
dep=dp;
st=s;
}
friend ostream& operator<<(ostream &os, const College &cl);
friend istream& operator>>(istream &is , College &cl);
void SetClgDetails()
{cin >> dep;cin >> st;}
};
ostream& operator<<(ostream &os, const College &cl)
{
os <<cl.m_ClgName<<" has "<<cl.m_numDept<<" departments\n";
cout <<cl.dep;
cout <<cl.st<<endl;
}
istream& operator>>(istream &is , College &cl)
{
cout << "\nEnter college name :";
is >> cl.m_ClgName;
cout << "\nEnter no. of departments: ";
is >> cl.m_numDept;
}
int main()
{
College clg("ABCD", 1, Department("Computer_Science"),Student("Rajender",249));
cout << clg;
cout << "===============================================";
cin >>clg;
clg.SetClgDetails();
cout <<clg;
return 0;
}
I also want to know how can I make array of objects of Department and Student in College class something like:
Department *dp = new Department[size_d];
Student *st= new Student[size_s];
where size_d and size_s will be objects of College class and will take user input to decide how many department and students user wants and how should constructor definition should be in these cases.