c++ printing object and constructor - c++

Hello everyone I had a problem while running the following program in the last loop it only print three 0 without displaying any result I am not sure what is wrong with the program,perhaps it has something to do with the constructor, however when I write my getter method in the second last loop with setter method it display the correct result. any help will be appreciated.
#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED
#include<string>
using namespace std;
class Employee
{
private:
string name;
int idnumber;
string department;
string position;
public:
Employee(string ,int ,string ,string);
void setName(string );
void setDepartment(string);
void setPosition(string);
void setIDNumber(int);
string getName() const
{
return name;
}
string getDepartment() const
{
return department;
}
string getPosition() const
{
return position;
}
int getIDnumber() const
{
return idnumber;
}
};
#include "Employee.h";
#include <iostream>
#include <iomanip>
using namespace std;
Employee::Employee(string name ,int IDNumber ,string department ,string position)
{
name=name;
idnumber=IDNumber;
department=department;
position=position;
}
void Employee::setDepartment(string Department)
{
department=Department;
}
void Employee::setName(string Name)
{
name=Name;
}
void Employee::setPosition(string Position)
{
position=Position;
}
void Employee::setIDNumber(int Number)
{
idnumber=Number;
}
int main()
{
string name;
int IDNumber;
string department;
string position;
const int Item=3;
Employee info1(" ",0," "," ");
Employee info2(" ",0," "," ");
Employee info3(" ",0," "," ");
Employee Info[Item]={info1,info2,info3};
}
for(Employee element:Info)
{
cout<<"please enter your name "<<endl;
cin>>name;
element.setName(name);
cout<<element.getName()<<endl;
cout<<"please enter your department "<<endl;
cin>>department;
element.setDepartment(department);
cout<<element.getDepartment()<<endl;
cout<<"please enter your position"<<endl;
cin>>position;
element.setPosition(position);
cout<<element.getPosition()<<endl;
cout<<"please enter your IDNumber"<<endl;
cin>>IDNumber;
element.setIDNumber(IDNumber);
cout<<element.getIDnumber()<<endl;
}
for(Employee element:Info)
{
cout<<element.getName()<<setw(8)<<element.getDepartment()
cout<<setw(8)<<element.getPosition()<<setw(8)<<element.getIDnumber()<<endl;
}
}

for(Employee element:Info)
element is a copy of an element from Info array. You are busy filling this copy, then it gets discarded. Contents of Info remain unchanged, with empty strings all around.
Make it
for(Employee& element:Info)
Note the ampersand.

Related

C++ Can not manage to display the class datatype array

This is the code I wrote, total beginner
#include <iostream>
#include <cstring>
using namespace std;
class Person
{
public:
string ID;
string name;
string surname;
string department;
string email;
public:
//get and set functions for ID, Name, Surname, Department, Email properties
string getID()
{
return this->ID;
};
void setID(string _ID)
{
this->ID = _ID;
};
string getName()
{
return this->name;
};
void setName(string _name)
{
this->name = _name;
};
string getSurname()
{
return this->surname;
};
void setSurname(string _surname)
{
this->surname = _surname;
};
string getDepartment()
{
return this->department;
};
void setDepartment(string _department)
{
this->department = _department;
};
string getEmail()
{
return this->email;
};
void setEmail(string _email)
{
this->email = _email;
};
};
//inherit Student class from Person class
class Student :public Person
{
private:
int student_counter = 0;
public:
//constructor
Student()
{
};
Student(string id, string Name, string Surname, string Department, string Email)
{
setID(id);
setName(Name);
setSurname(Surname);
setDepartment(Department);
setEmail(Email);
}
//student add
void addStudent(string id, string Name, string Surname, string Department, string Email)
{
if (student_counter >= 100)
{
cout << "cant add more students";
}
else
{
Student _S[100]
Student newS;
newS.setID(id);
newS.setName(Name);
newS.setSurname(Surname);
newS.setDepartment(Department);
newS.setEmail(Email);
_S[student_counter] = newS;
student_counter++;
}
}
//display students
void display()
{
for (int i = 0; i < student_counter; i++)
{
cout << _S[i].getID() << " - ";
}
}
};
};
int
main()
{
Student stu;
stu.addStudent("ST123456", "Ege", "Inan", "CS", "ege # gmail.com");
stu.display();
}
The problem is here
//inherit Student class from Person class
class Student :public Person
{
private:
int student_counter = 0;
public:
//constructor
Student()
{
};
Student(string id, string Name, string Surname, string Department, string Email)
{
setID(id);
setName(Name);
setSurname(Surname);
setDepartment(Department);
setEmail(Email);
}
//student add
void addStudent(string id, string Name, string Surname, string Department, string Email)
{
if (student_counter >= 100)
{
cout << "cant add more students";
}
else
{
Student _S[100];
Student newS;
newS.setID(id);
newS.setName(Name);
newS.setSurname(Surname);
newS.setDepartment(Department);
newS.setEmail(Email);
_S[student_counter] = newS;
student_counter++;
}
}
//display students
void display()
{
for (int i = 0; i < student_counter; i++)
{
cout << _S[i].getID() << " - ";
}
}
};
};
The error message I get is this:
main.cpp:106:9: error: ‘_S’ was not declared in this scope
106 | cout<<_S[i].getID()<<" - ";
| ^~
I tried moving the definition of Student _S to the private part of student class like this:
class Student :public Person
{
private:
int student_counter = 0;
Student _S[100];
public:
//constructor
Student()
{
};
Student(string id, string Name, string Surname, string Department, string Email)
{
setID(id);
setName(Name);
setSurname(Surname);
setDepartment(Department);
setEmail(Email);
}
//student add
void addStudent(string id, string Name, string Surname, string Department, string Email)
{
if (student_counter >= 100)
{
cout << "cant add more students";
}
else
{
Student newS;
newS.setID(id);
newS.setName(Name);
newS.setSurname(Surname);
newS.setDepartment(Department);
newS.setEmail(Email);
_S[student_counter] = newS;
student_counter++;
}
}
//display students
void display()
{
for (int i = 0; i < student_counter; i++)
{
cout << _S[i].getID() << " - ";
}
}
};
};
But then I got this:
65 | Student _S[100];
| ^~
main.cpp:61:7: note: definition of ‘class Student’ is not complete until the closing brace
61 | class Student:public Person
| ^~~~~~~
What would be the best way of handling this? where to define the array to get the program to run as intended?
In the first code, _S is a local variable inside of addStudent(), so display() can't access it.
Your second code is better, in that display() can now access _S. But a Student object can't hold any data members that are also Student objects, you would end up with an endless recursive allocation.
To accomplish what you want, you need to make _S, student_counter, addStudent(), and display() be static members of the Student class, eg:
class Student :public Person
{
private:
static int student_counter;
static Student _S[100];
public:
//constructor
Student()
{
};
Student(string id, string Name, string Surname, string Department, string Email)
{
setID(id);
setName(Name);
setSurname(Surname);
setDepartment(Department);
setEmail(Email);
}
//student add
static void addStudent(string id, string Name, string Surname, string Department, string Email)
{
if (student_counter >= 100)
{
cout << "cant add more students";
}
else
{
Student newS;
newS.setID(id);
newS.setName(Name);
newS.setSurname(Surname);
newS.setDepartment(Department);
newS.setEmail(Email);
_S[student_counter] = newS;
student_counter++;
}
}
//display students
static void display()
{
for (int i = 0; i < student_counter; i++)
{
cout << _S[i].getID() << " - ";
}
}
};
int Student::student_counter = 0;
Student Student::_S[100];
int main()
{
Student::addStudent("ST123456", "Ege", "Inan", "CS", "ege # gmail.com");
Student::display();
}
Online Demo

Passing an object as argument in c++

Iam new to c++ trying to learn about namespaces and class.
I have 2 classes here Both are in different files.
The first one is
#include<iostream>
using namespace std;
namespace project{
class Student{
string name,address,enrolmentDate ,usn;
int hours;
public:
Student(char* nusn, char* nname, char* naddress, char* ndate,int nhours){
usn = nusn;
name = nname;
address = naddress;
enrolmentDate = ndate;
hours = nhours;
}
Student(){
getData();
}
void getData(){
std::cout << "Enter Usn,Name,Address,EnrolmentDate,Hours\n";
std::cin >> usn >> name >> address >> enrolmentDate >> hours;
}
string getName(){
return name;
}
string getAddress(){
return address;
}
string getenrolmentDate(){
return enrolmentDate;
}
int getHours(){
return hours;
}
string getUsn(){
return usn;
}
};
}
The Second class is
#include<iostream>
using namespace std;
namespace project{
class CourseRegistration{
string usn, courseId ;
int hours, grade;
public:
CourseRegistration(project::Student obj, string courseId, int nhours, int ngrade){
usn = obj.getUsn();
this->courseId = courseId;
hours = nhours;
grade = ngrade;
}
};
}
The First class Compiles fine.But the second class gives an error.
The error is near that Student Object.
Course.cpp:10:38: error: expected ‘)’ before ‘obj’
CourseRegistration(project::Student obj, string courseId, int nhours, int ngrade){
How Do i Correct It?
I do not see any evidence you've included the definition of Student in "the second file." Your class should be delclared in a header (.h) file with the implementation in a source (.cpp) file. Your compiler is likely complaining because it doesn't know that Student is a class.
Student.h
#include<iostream>
using namespace std;
namespace project {
class Student {
private:
string name,address,enrolmentDate, usn;
int hours;
public:
Student( char*, char*, char*, char*, int );
Student();
void getData();
string getName();
string getAddress();
string getenrolmentDate();
int getHours();
string getUsn();
}
}
Student.cpp
#include "Student.h"
namespace project {
Student::Student( char* nusn, char* nname, char* naddress, char* ndate,int nhours ) {
usn = nusn;
name = nname;
address = naddress;
enrolmentDate = ndate;
hours = nhours;
}
Student() {
getData();
}
void getData() {
std::cout << "Enter Usn,Name,Address,EnrolmentDate,Hours\n";
std::cin >> usn >> name >> address >> enrolmentDate >> hours;
}
string getName() {
return name;
}
string getAddress() {
return address;
}
string getenrolmentDate() {
return enrolmentDate;
}
int getHours() {
return hours;
}
string getUsn() {
return usn;
}
}
CourseRegistration.h
#include "Student.h"
#include<iostream>
using namespace std;
namespace project{
class CourseRegistration {
private:
string usn, courseId ;
int hours, grade;
public:
CourseRegistration( Student, string, int, int );
}
}
CourseRegistration.cpp
#include<CourseRegistration.h>
namespace project {
CourseRegistration::CourseRegistration( Student obj, string courseId, int nhours, int ngrade ) {
usn = obj.getUsn();
this->courseId = courseId;
hours = nhours;
grade = ngrade;
}
}

Errors in class relationships in c++

I am quite new to the concept of class relationships and wrote a program to test it out. However, it is giving me some errors.
There are 5 header files for classes University,Dept,Teacher,Student and Course and a .cpp file involved. I have implemented composition between University and Dept and bidirectional association between Dept and Student, Teacher, Course
uni.h
#pragma once
//#include"dept.h"
class University
{
public:
University(string,int);
void setDepartmentName(string,int);
Dept* getDeptAddress(int);
~University();
private:
Dept* departments;
int totalDepts;
string name;
};
University::University(string name1,int num) :name(name1)
{
departments = new Dept[num];
totalDepts=num;
}
void University::setDepartmentName(string name,int depNo)
{
departments[depNo].setName(name);
}
Dept* University::getDeptAddress(int i)
{
return &(departments[i]);
}
University::~University()
{
delete[] departments;
}
dept.h
#pragma once
//class Student;
//class Teacher;
//class Course;
#include"course.h"
#include"teacher.h"
#include"student.h"
class Dept
{
public:
Dept();
string getName();
void setName(string);
~Dept();
private:
Student** students;
Course** courses;
Teacher** teachers;
string name;
int noOfStudents, noOfTeachers, noOfCourses;
};
Dept::Dept()
{
}
string Dept::getName() {
return name;
}
void Dept::setName(string name1) {
name = name1;
}
Dept::~Dept()
{
}
course.h
#pragma once
class Dept;
//#include"dept.h"
class Course
{
public:
Course(string, string);
void assignDept(Dept*);
string getDeptName();
~Course();
private:
Dept* department;
string code;
string name;
};
Course::Course(string code1, string name1) :code(code1), name(name1)
{}
void Course::assignDept(Dept * dep)
{
department = dep;
}
string Course::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Course::~Course()
{}
student.h
#pragma once
#include"dept.h"
class Student
{
public:
Student(string,string);
void assignDept(Dept*);
string getDeptName();
~Student();
private:
Dept* department;
string rollNo, name;
};
Student::Student(string rNo,string name1):name(name1),rollNo(rNo)
{}
void Student::assignDept(Dept *dep)
{
department = dep;
}
string Student::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Student::~Student()
{
}
teacher.h
#pragma once
//#include"dept.h"
class Teacher
{
public:
Teacher(string);
void assignDept(Dept*);
string getDeptName();
~Teacher();
private:
Dept* department;
string name;
};
Teacher::Teacher(string name1) :name(name1)
{}
void Teacher::assignDept(Dept *dep)
{
department = dep;
}
string Teacher::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Teacher::~Teacher()
{
}
source.cpp
#include<iostream>
using namespace std;
#include<string>
#include"dept.h"
#include"course.h"
#include"teacher.h"
#include"student.h"
#include"uni.h"
int main()
{
University u1("FAST",3);
u1.setDepartmentName("CS", 0);
u1.setDepartmentName("EE", 1);
u1.setDepartmentName("CV", 2);
Student s1("l144049", "Syed Abdul Wahab");
Course c1("cs1", "ITC");
Teacher t1("abc");
c1.assignDept(u1.getDeptAddress(0));
t1.assignDept(u1.getDeptAddress(1));
s1.assignDept(u1.getDeptAddress(2));
cout << c1.getDeptName()<<endl;
cout << t1.getDeptName() << endl;
cout << s1.getDeptName() << endl;
cin.get();
return 0;
}
However, if i #include 'dept.h' in student.h, course.h and teacher.h, it gives me errors on 'Dept' namely 'identifier 'Dept' is undefined'.
Any help would me greatly appreciated!
The problem is you have a circular dependency: teacher.h includes dept.h which includes teacher.h. This can't work.
To fix it, use "forward declarations" in your header files, and move your implementations to .cpp files.

Printing derived classes through a function

I'm new to c++ because all throughout our lab activities we just use C# and I'm still trying get the gist of it. So our last lab, which uses c++, is to show our genealogy-our family tree- and my problem right now is printing. Can someone point out what's wrong in my function or why it's not printing and what's the best way to do it?
#include <iostream>
#include <string>
using namespace std;
class parent
{
public:
void setMom(string mom)
{
mother=mom;
}
void setDad(string dad)
{
father=dad;
}
string getDad(void)
{
return father;
}
string getMom(void)
{
return mother;
}
protected:
string mother;
string father;
};
class Member: public parent
{
public:
string name;
Member()
{
}
void setName(string kid)
{
name=kid;
}
void setStatus(string stat)
{
status=stat;
}
void setAge(int num)
{
age=num;
}
string getName()
{
return name;
}
private:
string status;
int age;
};
char addPerson()
{
Member person;
char mom[50];
char dad[50];
char kid[50];
char stat[7];
int num;
cout<<"Name: ";
cin>>kid;
person.setName(kid);
cout<<"Age: ";
cin>>num;
person.setAge(num);
cout<<"Status(Living/Diseased): ";
cin>>stat;
person.setStatus(stat);
cout<<"Father: ";
cin>>dad;
person.setDad(dad);
cout<<"Mother: ";
cin>>mom;
person.setMom(mom);
}
my function for printing.
void showme()
{
Member person;
cout<<person.getMom();
}
//-----------------------------------------------------MAIN--------------------------------------------------
int main()
{
while(1)
{
int choice;
cout<<" 1. Add member"<<"\n";
cout<<" 2. Edit member"<<"\n";
cout<<" 3. Show family tree"<<"\n";
cout<<" 4. Exit"<<"\n";
cin>>choice;
if(choice==1)
{
addPerson();
}
else if(choice==2)
{
}
else if(choice==3)
{
showme();
}
else if(choice==4)
{
break;
}
}
}

Class Constructor error, class "classname" has no member "classname"

#ifndef RESERVATIONS_H_INCLUDED
#define RESERVATIONS_H_INCLUDED
#include <vector>
#include <string.c>
class Reservations
{
public:
Reservations::Reservations { }
Reservations(string FullName, int PhoneNum);
string getname() { return FullName; }
int getnumber() { return PhoneNum; }
private:
string FullName;
int PhoneNum;
}
#endif // RESERVATIONS_H_INCLUDED
Reservations::Reservations(string FullName, int PhoneNum) //error on this line
{
FullName = FullName;
PhoneNum = PhoneNum;
}
I get the error in the title, I don't know why it's assuming I want it to have a member of it's own class...
you included wrong header file
Change
#include <string.c>
to
#include <string>
Also use string with full namespace std.
below code should compile with minor fix:
class Reservations
{
public:
Reservations() : PhoneNum(0) {}
Reservations(std::string FullName, int PhoneNum);
std::string getname() { return FullName; }
int getnumber() { return PhoneNum; }
private:
std::string FullName;
int PhoneNum;
};
Reservations::Reservations(std::string FullName, int PhoneNum)
{
this->FullName = FullName;
this->PhoneNum = PhoneNum;
}
// Better use member initializers list
Reservations::Reservations(std::string FullName, int PhoneNum)
: FullName(FullName),
PhoneNum(PhoneNum)
{
}
Do you mean
Reservations::Reservations() { }
instead of
Reservations::Reservations { }
?
Do this:
class Reservations
{
public:
//Reservations::Reservations { }
Reservations() { }
....