// main.cpp
#include <iostream>
#include <vector>
#include <string>
#include "Student.h"
using namespace std;
void fillVector(vector<Student>&);
void printVector(const vector<Student>&);
int main()
{
vector<Student> myClass;
fillVector(myClass);
printVector(myClass);
return 0;
}
void fillVector(vector<Student>& newMyClass)
{
string name;
char grade;
cout << "How many you students are in your class? ";
int classSize;
cin >> classSize;
for (int i=0; i<classSize; i++)
{
cout<<"Enter student name: ";
cin>>name;
cout<<"Enter student grade: ";
cin>>grade;
Student newStudent(name, grade);
newMyClass.push_back(newStudent);
cout<<endl;
}
cout<<endl;
}
void printVector(const vector<Student>& newMyClass)
{
int size = newMyClass.size();
for ( int i=0; i<size; i++ )
{
cout<<"Student name: "<<newMyClass[i].getName()<<endl;
cout<<"Student grade: "<<newMyClass[i].getGrade()<<endl;
cout<<endl;
}
}
// Student.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student();
Student(string, char);
~Student();
string getName() const;
char getGrade() const;
void setName(string);
void setGrade(char);
private:
string newName;
char newGrade;
};
#endif // STUDENT_H_INCLUDED
// Student.cpp
#include "Student.h"
Student::Student()
{
newGrade = ' ';
}
Student::Student(string name, char grade)
{
newName = name;
newGrade = grade;
}
Student::~Student() {}
string Student::getName() const
{
return newName;
}
char Student::getGrade() const
{
return newGrade;
}
void Student::setName(string name)
{
newName = name;
}
void Student::setGrade(char grade)
{
newGrade = grade;
}
My aim is to declare a class called CreateGradeBook inside Student.hand define it in Student.cpp and put all the code of the main.cpp in it. In other words I want main.cpp to be still there but with no codes in it like below;
#include <iostream>
using namespace std;
int main()
{
}
Please be tolerant if my question is inappropriate or off topic as I'm fairly new with StackOverflow. I've read FAQ section but not all of it.
Well here's a start
class CreateGradeBook
{
public:
void fillVector();
void printVector() const;
private:
vector<Student> myClass;
};
Notice how the vector you declared in main has become a private data member myClass. The two functions you declared have become public methods of CreateGradeBook. Notice also that they have lost their parameters instead they'll now operate on the private data member myClass.
I'll leave you to do the rest.
Related
The question:
Why is the following error happening?
definition of implicity-declared 'Clothing::Clothing()
The context:
As an assignment I have to do constructors, destructors and methods in a class Clothing. I'm having a problem when I try to define the constructor in clothing.cpp. I have read that the problem is because I did not declare the constructor in clothing.h, but I think how I have done it, it's declared. I have no clue where the problem lies.
My code:
clothing.h:
#ifndef CLOTHING_H_
#define CLOTHING_H_
#include <string>
#include <iostream>
using namespace std;
class Clothing {
private:
int gender;
int size;
string name;
public:
Clothing();
Clothing(const Clothing &t);
Clothing(int gender, int size, string name);
~Clothing();
int getGender();
int getSize();
string getName();
void setGender(int gender1);
void setSize(int size1);
void setName(string name1);
void print();
void toString();
};
#endif /* CLOTHING_H_ */
clothing.cpp:
#include <iostream>
#include "clothing.h"
#include <string>
#include <sstream>
using namespace std;
Clothing::Clothing() :
gender(1),
size(1),
name("outofstock") {
}
Clothing::Clothing(const Clothing& t) :
gender(t.gender),
size(t.size),
name(t.name) {
}
Clothing::Clothing(int gender, int size, string name) {
}
int Clothing::getGender() {
return gender;
}
int Clothing::getSize() {
return size;
}
string Clothing::getName() {
return name;
}
void Clothing::setGender(int gender1) {
gender = gender1;
}
void Clothing::setSize(int size1) {
size = size1;
}
void Clothing::setName(string name1) {
name = name1;
}
void Clothing::print() {
cout << name << " " << gender << " " << size << endl;
}
void Clothing::toString() {
stringstream ss;
ss << name << " " << gender << " " << size;
cout << ss.str();
}
Errors: \src\clothing.cpp:7:21: error: definition of implicitly-declared 'Clothing::Clothing()'
\src\clothing.cpp:14:37: error: definition of implicitly-declared 'Clothing::Clothing(const Clothing&)'
The error is: you declared a destructor but you didn't define it. Add a definition for the destructor or define it as default:
#ifndef CLOTHING_H_
#define CLOTHING_H_
#include <string>
#include <iostream>
using namespace std;
class Clothing {
private:
int gender;
int size;
string name;
public:
Clothing();
Clothing(const Clothing &t);
Clothing(int gender, int size, string name);
~Clothing() = default; // <-- add a default destructor
int getGender();
int getSize();
string getName();
void setGender(int gender1);
void setSize(int size1);
void setName(string name1);
void print();
void toString();
};
#endif /* CLOTHING_H_ */
After fixing this your code snippet works: tio.run
If you have more problems with your code, the problems are outside of your provided code snippet.
The ERROR! starts from line 7 which is GradeBook::GradeBook(string name)(Type name is not Allowed)
I Wrote it the same as in the example cant understand why its not working and I am using visual studio 2015 to compile.
#include <iostream>
#include "Header.h"
using namespace std;
int main() {
GradeBook::GradeBook (string name) {
setCourseName(name);
}
void GradeBook::setCourseName(string name) {
coursename = name;
}
string GradeBook::getCourseName() {
return courseName;
}
void GradeBook::displayMessage() {
cout << "Welcome to the Grade Book\n" << getCourseName <<"!" << endl;
}
system("PAUSE");
return 0;
}
You need to move the definitions of the member functions of your class outside main.
#include <iostream>
#include "Header.h"
using namespace std;
GradeBook::GradeBook (string name) {
setCourseName(name);
}
void GradeBook::setCourseName(string name) {
coursename = name;
}
string GradeBook::getCourseName() {
return courseName;
}
void GradeBook::displayMessage() {
cout << "Welcome to the Grade Book\n" << getCourseName <<"!" << endl;
}
int main() {
system("PAUSE");
return 0;
}
We cannot define functions inside the another function in c++.
int main() {
GradeBook::GradeBook (string name) {
setCourseName(name);
}
void GradeBook::setCourseName(string name) {
coursename = name;
}
return 0;
}
but can declare function in another function.
and any other IDE also don't allow to define function in another function. You can define like this.
GradeBook::GradeBook (string name) {
setCourseName(name);
}
void GradeBook::setCourseName(string name) {
coursename = name;
}
int main(){
GradeBook b;
return 0;
}
I have to create a small console application in C++ which will do the following:
Create class Subject which has next attributes: name of the subject, number of students and array of students who are attending that subject. After that createa class Student which has name and surname of the student as attributes. In Main file count how many duplicate names there are in each Subject.
I have few problems here. First one is that I don't know how to initialize an array in my Subject.h file. Second is how to actually put Student objects into Subject objects and compare the names in the end.
What I'd like output to look like:
Duplicate names in subjectA are: Michael.
Duplicate names in subjectB are: Nicholas, John.
Where subjectA and subjectB should be called C++ and C.
Here is my code so far (I googled for past hour or two about this problem of mine but I just couldn't find a proper answer/example).
NOTE: I'm including all these files for clarification.
Subject.h
#include <string>
#include <iostream>
using namespace std;
/*
* I should also have an array named `arrayOfStudents`
* which should store all students who are attending
* that Subject.
*/
class Subject
{
public:
Subject();
Subject(Subject &subject);
Subject(string nameOfSubject, int numberOfStudents);
~Subject();
const string getNameOfSubject();
const int getNumberOfStudents();
void setNameOfSubject(string nameOfSubject);
void setNumberOfStudents(int numberOfStudents);
void print();
private:
string nameOfSubject;
int numberOfStudents;
};
Subject.cpp
#include <iostream>
#include "Subject.h"
using namespace std;
Subject::Subject()
{
}
Subject::Subject(string nameOfSubject, int numberOfStudents)
{
this->nameOfSubject = nameOfSubject;
this->numberOfStudents = numberOfStudents;
}
Subject::Subject(Subject &Subject) : nameOfSubject(Subject.getNameOfSubject()), numberOfStudents(Subject.getNumberOfStudents())
{
}
Subject::~Subject()
{
cout << "Object is destroyed" << endl;
}
const string Subject::getNameOfSubject()
{
return nameOfSubject;
}
const int Subject::getNumberOfStudents()
{
return numberOfStudents;
}
void Subject::setNameOfSubject(string nameOfSubject)
{
nameOfSubject = this->nameOfSubject;
}
void Subject::setNumberOfStudents(int numberOfStudents)
{
numberOfStudents = this->numberOfStudents;
}
void Subject::print()
{
cout << "Subject: " << nameOfSubject << " :: Number of students: " << numberOfStudents << endl;
}
Student.h
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
Student();
Student(Student &student);
Student(string name, string surname);
~Student();
const string getName();
const string getSurname();
void setName(string name);
void setSurname(string surname);
void print();
private:
string name;
string surname;
};
Student.cpp
#include <iostream>
#include "Student.h"
using namespace std;
Student::Student()
{
}
Student::Student(string name, string surname)
{
this->name = name;
this->surname = surname;
}
Student::Student(Student &student) : name(student.getName()), surname(student.getSurname())
{
}
Student::~Student()
{
cout << "Object is destroyed" << endl;
}
const string Student::getName()
{
return name;
}
const string Student::getSurname()
{
return surname;
}
void Student::setName(string name)
{
name = this->name;
}
void Student::setSurname(string surname)
{
surname = this->surname;
}
void Student::print()
{
cout << "Student: " << name << " " << surname << endl;
}
Main.cpp
#include <iostream>
#include "Subject.h"
#include "Student.h"
using namespace std;
int main()
{
/*
* First three students should attend first Subject
* while other four the second Subject.
* Also note that only names matter and not surnames.
*/
Student stA("Michael", "Doe");
Student stB("Michael", "Doe");
Student stC("Thomas", "Doe");
Student stD("Nicholas", "Doe");
Student stE("Nicholas", "Doe");
Student stF("John", "Doe");
Student stG("John", "Doe");
Subject subjectA("C++", 3);
Subject subjectB("C", 4);
return 0;
}
1) Get an array of Students into your Subject object: you probably want to use vectors instead of arrays here:
in subject.h add
#include "Student.h"
public:
void addStudent(Student student);
private:
std::vector<Student> students_;
in subject.cpp add
void Subject::addStudent(Student student)
{
this->students_.push_back(student);
}
If you want to extract the student list somehow later, you need to write a function to access it (or make it public).
2) For finding duplicates, look here
Checking for duplicates in a vector
You have to pay attention: the Student objects are in your subject object, not the student names. You have to extract them first and e.g. put them in a vector.
Your task definition sais you should have an array of Students attribute of the Subject class, but i don't see this in your Subject class definition.
And maybe an add Student method and then iterate over the array.
I'm trying to write a program that takes the grades and prints out the following:
ID:123 NAME:John GRADE:78
but instead I'm getting:
ID:-842150451 NAME: GRADE: 78
Can you guys help me and give me some extra tips to make my code cleaner since I'm fairly new to C++.
Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
Student(int num, string text);
int getID();
void setExamGrade(int a, int b);
int getOverallGrade();
void display();
string getName();
string name;
int id;
int exams[3];
int sum;
int average;
};
#endif
Student.cpp
#ifndef STUDENT_CPP
#define STUDENT_CPP
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
Student::Student(int num, string text)
{
num = id;
text = name;
exams[0, 1, 2] = 0;
}
int Student::getID() {
return id;
}
string Student::getName() {
return name;
}
void Student::setExamGrade(int a, int b) {
exams[a] = b;
}
int Student::getOverallGrade() {
sum = exams[0] + exams[1] + exams[2];
average = sum / 3;
return average;
}
void Student::display() {
cout << "ID: " << getID();
cout << " NAME: " << getName();
cout << " GRADE: " << getOverallGrade() << endl;
}
#endif
gradebook.cpp
#ifndef GRADEBOOK_CPP
#define GRADEBOOK_CPP
#include "Student.h"
#include <iostream>
using namespace std;
int main() {
Student *s = new Student(123, "John");
s->setExamGrade(0, 80);
s->setExamGrade(1, 60);
s->setExamGrade(2, 95);
s->display();
delete s;
return 0;
}
#endif
You never assign to id in the constructor, hence it's uninitialized and you will have undefined behavior when you print it.
Change
num = id;
to
id = num;
Same with the name.
Also, the statement
exams[0, 1, 2] = 0;
doesn't do what you expect it to do, it only initializes exams[2] to sero, and leaves the rest uninitialized. The expression 0, 1, 2 uses the comma operator.
Either assign to all members of the array separately, or use a constructor member initializer list (which I recommend for all the initialization).
So Ive been learning about classes, and in my main function when I run it, it shows character array members incorrectly.
main program:
#include <iostream>
#include "account.h"
#include "account.cpp"
using namespace std;
int main(){
char num [] = "2435457";
char name [] = "BOB JOE";
account bob(10000, num, name );
bob.show_account();
cout << num; // this should output correctly, but shows the '♦'
return 0;
}
output:
ACCOUNT INFO:
Account holder :
Account number :♦
Balance :10000
♦
Whats weird is that using cout<< directly on the char array num shows the '♦'.
When I copy the char num [] = "2435457" into a new program like:
#include <iostream> //including all the same headers does not affect
#include "account.h" //the output
#include "account.cpp"
using namespace std;
int main(){
char num [] = "2435457";
cout << num;
return 0;
}
It works correctly.
EDITED :
Here is the header "account.h"
#ifndef BANK_H_
#define BANK_H_
using namespace std;
class account {
static const int NMAX = 20, AMAX = 10;
private:
double balance;
char account_number [AMAX];
char account_holder [NMAX];
public:
account(double BAL, char acct [], char name []);
void show_account();
void deposit(double money);
void withdrawl(double money);
};
#endif
And "account.cpp"
#include "account.h"
#include <iostream>
#include <cstring>
using namespace std;
account::account(double BAL, char acct[], char name[]){
strcpy(name, account_holder);
strcpy(acct, account_number);
balance = BAL;
}
void account::show_account(){
cout << "ACCOUNT INFO :"<<endl;
cout << "\tAccount holder :";print(account_holder);
cout << "\n\tAccount number :";print(account_number);
cout << "\n\tBalance :"<<balance<<endl;
}
void account::deposit(double money){
balance += money;
}
void account::withdrawl(double money){
balance -= money;
}
Your problem is in the constructor like I thought.
account::account(double BAL, char acct[], char name[]){
strcpy(name, account_holder);
strcpy(acct, account_number);
balance = BAL;
}
strcpy's reference says:
char* strcpy( char* dest, const char* src );
so you got the 2 parameters switched, you're copying from the uninitialized member array into your char pointer. Since the member array is uninitialized at that point reading from it is undefined behavior. You should switch them:
account::account(double BAL, char acct[], char name[]){
strcpy(account_holder, name);
strcpy(account_number, acct);
balance = BAL;
}