#include <iostream>
#include <cstring>
using namespace std;
class Film {
private:
string name;
int year_prod;
string producer;
string main_actor;
public:
Film();
void print()
{
cout << "\nThe name of movie: " << name;
cout << "\nThe year of produced: " << year_prod;
cout << "\nProducer: " << producer;
cout << "\nMain actor: " << main_actor << endl;
}
void SetName(string xName)
{
name = xName;
}
string GetName()
{
return name;
}
void SetYearP(int xYearP)
{
year_prod = xYearP;
}
int GetYearP()
{
return year_prod;
}
void SetProducer(string xProducer)
{
producer = xProducer;
}
string GetProducer()
{
return producer;
}
void SetMaina(string xMaina)
{
main_actor = xMaina;
}
string GetMaina()
{
return main_actor;
}
};
int main()
{
Film obs[100]; // maximum of 100 hundred films
int n;
cout << "how many films ";
cin >> n;
for (int i = 0; i < n; ++i)
{
string name;
int year;
string prod;
string actor;
cout << "enter the film name ";
cin >> name;
cout << "enter the production year ";
cin >> year;
cout << "enter the producer name ";
cin >> prod;
cout << "enter the actor name ";
cin >> actor;
obs[i].SetName(name);
obs[i].SetYearP(year);
obs[i].SetProducer(prod);
obs[i].SetMaina(actor);
}
}
I've done half of my code but I get errors while compiling saying: unresolved external symbol "public: __thiscall Film::Film(void)" (??0Film##QAE#XZ) referenced in function _main AND 1 unresolved externals. I'm not sure if I had in the correct way objects of n Film from user input because I'm still a beginner in OOP.
You didn't implement your constructor:
public:
Film();//<-- declared, but not defined.
If you don't want your constructor to do anything, just add an empty body:
public:
Film() {};
or even better, explicitly declare it as the default-constructor:
public:
Film()=default;
Please try this out. Hope this helps.
class Film {
...
...
...
public:
//Film(); Remove this line if you are not defining Film()
void print()
{
cout << "\nThe name of movie: " << name;
cout << "\nThe year of produced: " << year_prod;
cout << "\nProducer: " << producer;
cout << "\nMain actor: " << main_actor << endl;
}
...
...
...
}
If you want it to be a default constructor add an empty block Film(){}
You have to implement the constructor for your class. The constructor is the method that create an instance of the class. The set functions have the purpose to modify the attributes of an object, but the initialization of the attributes must be done in the constructor.
For example:
public:
//default constructor: it does not take
//anything as input, it sets name as an
//empty string and the year to 1900
Film(){
name = "";
year = 1900;
}
// It creates a Film object, whose name
// is NAME and whose year is YEAR
Film(string NAME, int YEAR){
name = NAME;
year = YEAR;
}
// set function which allows to modify the
// year of a Film object.
void setYear(int newYear){
year = newYear;
}
Have a look here for a quick introduction.
EDIT: you may want to set the default constructor as
Film(){};
In this way you have to invoke all the set functions to initialise its attributes.
Related
I'm trying to write a C++ code for a course I'm enrolled in, where I keep the information of the students enrolled in the course.
I should be able to add a student to the classrrom in the user interface written in main , by calling the function void addNewStudent(int ID, string name, string surname), where I create my object instances, Student, and Course inside the function.
I should also be able to search by given ID by calling the function void showStudent(int ID) in the main, where the function uses the getStudent(ID) method of the object of the classCourse
I did not write all the methods, but when I try to debug this code, I got the error " Exception has occured, unknown signal error."
My questions are:
What is the reason of this error? How can I fix it?
Suppose that the user interface in the main is necessary to use as well as the functions it calls. Do I have to create a class object again inside each function as I wrote?
Can a more effective implementation be made in accordance with the object oriented principles I have defined above?
#include <iostream>
using namespace std;
#define MAX 10
class Student {
private:
int ID;
string name;
string surname;
public:
Student()
{
ID = 0;
string name = "" ;
string surname = "";
}
void setID(int ID_set);
int getID();
void setName(string name_set);
string getName();
void setSurName(string surname_set);
string getSurName();
};
class Course {
private:
Student students[MAX];
int num =0 ; // The current number of students in the course, initially 0.
float weightQ;
float weightHW;
float weightF;
public:
Course()
{
students[num] = {};
weightQ = 0.3;
weightHW = 0.3;
weightF = 0.4;
}
int getNum(); // Returns how many students are in the course
void addNewStudent(Student new_student);
void updateWeights(float weightQ_update, float weightHW_update, float weightF_update);
void getStudent(int ID_given);
};
// Method declerations for the class Student
void Student :: setID(int ID_set){
ID = ID_set;
}
int Student :: getID(){
return ID;
}
void Student :: setName(string name_set){
name = name_set;
}
string Student :: getName(){
return name;
}
void Student :: setSurName(string surname_set){
surname = surname_set;
}
string Student :: getSurName(){
return surname;
}
// Method declerations for the class Course
int Course :: getNum(){
return num;
}
void Course :: addNewStudent(Student new_student){
students[num] = new_student ;
num = num + 1;
}
void Course :: updateWeights(float weightQ_update, float weightHW_update, float weightF_update){
weightQ = weightQ_update;
weightHW = weightHW_update;
weightF = weightF_update;
}
void Course :: getStudent(int ID_given){
for(int i = 0; i<MAX; i++){
if(ID_given == students[i].getID()){
cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
}
}
}
void addNewStudent(int ID, string name, string surname){
Student student;
Course ECE101;
student.setID(ID);
student.setName(name);
student.setSurName(surname);
ECE101.addNewStudent(student);
}
void showStudent(int ID){
Course ECE101;
ECE101.getStudent(ID);
}
int main(){
Course ECE101;
cout << "Welcome to the ECE101 Classroom Interface"<<"\n";
cout << "Choose your option\n";
string option_1 = "1) Add a student ";
string option_2 = "2) Search a student by ID";
cout << "Enter your option: ";
int x;
int ID;
string name, surname;
cin >> x;
if (x == 1)
cout << "Enter the student ID ";
cin >> ID;
cout << endl;
cout << "Enter the student name ";
cin >> name;
cout << endl;
cout << "Enter the student surname " ;
cin >> surname;
addNewStudent(ID, name, surname);
return 0;
}
To make the menu more interactive you could add a do while statement that would accept 3 options:
register
show data
exit
int main(){
Course ECE101;
int x;
int ID;
string name, surname;
string option_1 = "1) Add a student\n";
string option_2 = "2) Search a student by ID\n";
cout << "Welcome to the ECE101 Classroom Interface\n";
cout << "Choose your option\n";
cout << option_1 << option_2;
cin >> x;
do {
if (x == 1) {
cout << "Enter the student ID ";
cin >> ID;
cout << endl;
cout << "Enter the student name ";
cin >> name;
cout << endl;
cout << "Enter the student surname " ;
cin >> surname;
addNewStudent(ID, name, surname, ECE101);
}
else {
cout << "Enter the student ID\n";
cin >> ID;
showStudent(ID, ECE101);
}
cout << "Choose your option\n";
cin >> x;
} while(x != 3);
return 0;
}
addnewStudent() and showStudent() methods now accepts an instance of Course as an argument to be able to add students.
void addNewStudent(int ID, string name, string surname, Course &course) {
Student student;
student.setID(ID);
student.setName(name);
student.setSurName(surname);
course.addNewStudent(student);
}
void showStudent(int ID, Course &course) {
course.getStudent(ID, course);
}
the function is modified from the same class as well.
void Course::getStudent(int ID_given, Course &course) {
for(int i = 0; i<MAX; i++){
if(ID_given == students[i].getID()){
cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
}
}
}
Demo
Your addNewStudent function creates a new course everytime it is called. You could pass a reference to the course as a parameter into the function and call Course.addNewStudent(student). You'll want to make sure you specify it's a reference though when you define your function or you'll just create a copy of the course.
I tried this code but when I am calling the member function inside the loop it is giving the garbage value of the details and when I am calling the member function outside the loop it is giving me error.
#include<iostream>
#include<string.h>
using namespace std;
class student
{
char name[10];
int id,rollno;
public:
student(char name[10],int id,int rollno)
{
strcpy(this->name,name);
this->id=id;
this->rollno=rollno;
cout<<"the name of the student is:"<<name<<endl;
cout<<"the id of the student is:"<<id<<endl;
cout<<"the roll no of the student is:"<<rollno<<endl;
}
};
int main()
{
int id1,rollno1;
char name1[10];
for(int i=1;i<=2;i++)
{
cout<<" enter the detail of the student "<<i<<" "<<endl;
cout<<"enter the name of the student:";
cin>>name1;
cout<<"enter the id of the student:";
cin>>id1;
cout<<"enter the roll no of the student:";
cin>>rollno1;
student d[]={student(name1,id1,rollno1)};
d[i].print();
}
return 0;
}
Here's your code review.
#include <iostream>
#include <string.h>
using namespace std; // it is strongly suggested that you don't use the whole header unless your intent is speeding up coding
class Student // capitalize classes
{
char _name[10];// it is ok, however, if you use the <string> header, why would you go back to char type?
int _id, _rollno;
public:
Student(char name[10] /* this is just the array's first item passed! */, int id, int rollno)
{
// Don't use this->, use an underscore for one of the names.
// I use underscores for the encapsulated data
strncpy_s(_name, name, 9);// a safer version
_id = id;
_rollno = rollno;
cout << "\nCtor says: ";
cout << "the name of the student is: " << _name << endl;
cout << "the id of the student is: " << _id << endl;
cout << "the roll no of the student is: " << _rollno << endl;
cout << '\n';
}
const char* getName() { return _name; }
const int getId() { return _id; }
const int getRollNo() { return _rollno; }
// unless you overload operator<< , you have to make getters for your info, or make it public
};
int main()
{
int id, rollno;// why 1? they won't intersect with the function
char name[10];
Student *s[2];//you must make that on the heap; otherwise you need a default constructor
for (int i{ 0 }; i < 2; i++)// start with 0 and make it just <
{
cout << "enter the details of the student " << i << endl;// Why so many spaces?
cout << "enter the name of the student: "; // add space after the colon
cin >> name;// remember, you're limited to 9 chars + \n !!!
cout << "enter the id of the student: ";
cin >> id;
cout << "enter the roll no of the student: ";
cin >> rollno;
//student d[] = { student(name,id,rollno) }; // you can't do this. It's not flexible.
// You either assign enough space for the intended number of students in the stack, statically, or
// you lett it find more space in the heap = dynamically
//this goes to the heap
s[i]= new Student( name,id,rollno );// parentheses are well
//s[i] = new Student{ name,id,rollno };// initializer list may also do
//d[i].print();// what's to print here? It's not POD, cout needs instructions
cout << "Stored data -> Id: " <<
s[i]->getId() << ", Name: " // -> dereferences the pointer, and s is a pointer now
<< s[i]->getName() << ", Roll no: "
// or you can dereference it
<< (*s[i]).getRollNo()
<< endl << endl; // make some extra space here
}
return 0;
}
I'm not really sure if the title is correct but i have the following piece of code:
#include <iostream>
#include <string>
using namespace std;
class department
{
private:
string name;
int budget;
public:
int NoT; //Number of teachers
int NoS; //Number of students
void setName(string nam);
void setBudget(int budg);
int getBudget ();
string getName();
};
class school
{
private:
int YoE;
string name;
public:
department *dpt;
int NoDpts;
school();
};
void department::setName(string nam)
{
name=nam;
}
void department::setBudget(int budg)
{
budget=budg;
}
int department::getBudget()
{
return budget;
}
string department::getName()
{
return name;
}
school::school()
{
YoE=2000; // Year of estabilishment
name="Somename";
NoDpts=37;
}
int main()
{
string name;
int budget;
int budg;
school *sch;
sch=new school;
if (!sch)
throw("Out of memory");
sch->dpt=new department[37];
if (!sch->dpt)
throw("Out of memory");
for (int i=0;i<sch->NoDpts;i++)
{
cout << "Insert name for department#" << i+1 << ": ";
cin >> name;
sch->dpt->setName(name);
cout << "Insert budget for department " << name << ": ";
cin >> budget;
sch->dpt->setBudget(budget);
cin.ignore();
}
cout << "Insert a budget for school: ";
cin >> budget;
for (int i=0;i<sch->NoDpts;i++)
{
if (budg=sch->dpt->getBudget()>=budget)
{
cout << "Name of department is: " << sch->dpt->getName() << endl;
cout << "Budget of department amounts to: " << sch->dpt->getBudget() << endl;
cout << endl;
}
}
return 0;
}
As you can see, i allocated 37 departments, however i don't know how to access them.I mean that in the following line:
sch->dpt->setName(name);
I merely access the name of the first department (indirectly by using an accessor function),thus overwriting it and getting different results than expected.This happens to other members too such as
sch->dpt->setBudget(budget);
So i am simply asking how i can access the members of the rest of the departments.
I made 2 function : getdata, print for input/print variable in person class
#include <iostream>
#include <string>
using namespace std;
class person {
public:
char name[19];
int born_year, married_year;
};
void getdata(char* name, int born_year,int married_year) {
cout << "Enter Name!\n > ";
cin.getline(name, 19);
cout << "What's the Born Year?\n > ";
cin >> born_year;
cout << "What's the Married Year?\n > ";
cin >> married_year;
}
void print(char* name, int born_year, int married_year) {
cout << "Your Name : " << *name;
cout << "\n Your Born Year : " << born_year;
cout << "\n Your Married Year : " << married_year;
}
int main(void) {
person p;
int born_year, married_year;
getdata(p.name, p.born_year, married_year);
print(p.name, p.born_year, p.married_year);
return 0;
}
I'm trying to have input in a function outside the class
(class only have variables about person)
and print with with other function.
How could I make this work?
Your problem is in the variable types to your getdata function. The integer parameters need to be pass-by-reference (either pointers or references, the latter being preferred). Right now you pass in a copy of the born_year and married_year values. When you get the input you save that into local variables; the passed parameters are never updated.
You want something like this:
void getdata(char* name, int &born_year,int &married_year)
With that tiny change (adding the two & characters) your code should work as you expect.
#include <iostream>
#include <string>
using namespace std;
class person {
public:
char name[19];
int born_year, married_year;
};
void getdata(char* name, int &born_year, int &married_year) {
cout << "Enter Name!\n > ";
cin.getline(name, 19);
cout << "What's the Born Year?\n > ";
cin >> born_year;
cout << "What's the Married Year?\n > ";
cin >> married_year;
}
void print(char* name, int born_year, int married_year) {
cout << "Your Name : " << name;
cout << "\n Your Born Year : " << born_year;
cout << "\n Your Married Year : " << married_year;
}
int main(void) {
person p;
getdata(p.name, p.born_year, p.married_year);
print(p.name, p.born_year, p.married_year);
return 0;
}
If you want passed variables to be changed inside a function you need to pass them by reference. To do that you need to make some changes in your function definition:
void getdata(char* &name, int &born_year, int &married_year) {
//things you function does
}
Everything looks fine. Just you need to call 'function by reference' to directly change values of class variable.
https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm
void getdata(char* name, int born_year,int married_year)
you can remove variables declared in main function and use p. before every parameter.
getdata(p.name, p.born_year, p.married_year);
As others have mentioned your problem was that you were passing copies of the values in, not the variables themselves. Also you forgot to put the "p" on "married_year" in your main loop.
However, rather than passing in the separate values by reference, a much nicer idea is passing in a reference to the whole person:
int main(void) {
person p;
getdata(p);
print(p);
return 0;
}
Getdata and print would look like this:
void getdata(person &p) {
cout << "Enter Name!\n > ";
cin.getline(p.name, 19);
cout << "What's the Born Year?\n > ";
cin >> p.born_year;
cout << "What's the Married Year?\n > ";
cin >> p.married_year;
}
void print(const person &p) {
cout << "Your Name : " << *(p.name);
cout << "\n Your Born Year : " << p.born_year;
cout << "\n Your Married Year : " << p.married_year;
}
It's a cleaner way to do it, and since you're not creating or passing as many variables around, there are less ways to make a mistake here. If you made the fields private however, you'd need to make the two functions "friend" of the class to access its members.
I am new in programming and i wanted to know how can I input data from keyboard from class. Anyone?
#include <iostream>
#include <string>
using namespace std;
class Human{
private:
string *name;
int *age;
public:
Human(string iname, int iage){
name = new string;
age = new int;
*name = iname;
*age = iage;
}
void display(){
cout << "Hi I am " << *name << " and I am " << *age << " years old" << endl;
}
~Human(){
delete name;
delete age;
cout << "Destructor!";
}
void input(string, int)
{
string name;
int age;
cout << "Name: "; cin >> name;
cout << "Age: "; cin >> age;
}
};
int main()
{
Human *d1 = new Human(Human::input(?????????????????));
d1->display();
delete d1;
return 0;
}
EDIT:
I understand what I can do this:
int main()
{
Human *d1 = new Human("David",24);
d1->display();
return 0;
}
And this:
int main()
{
string name;
int age;
cout << "Name: "; cin >> name;
cout << "Age: "; cin >> age;
Human *d1 = new Human(name,age);
d1->display();
return 0;
}
But I want to know how can I put the data from keyboard with an input function.
Zygis you need to read a tutorial for the basics in C++. Pointers, the *, is a powerful thing programmers can use, but only when they need to. In this case, you do not need to. When do you need them? I think you should leave that for later and focus on my example below. When you understand that, you can read about Pointers in the internet.
#include <iostream>
#include <string>
using namespace std;
class Human {
private:
// data members of class
string name;
int age;
public:
// constructor without arguments
// useful for initializing the data members
// by an input function
Human() {
name = ""; // empty string
age = -1; // "empty" age
}
// constructor with arguments
// useful when you know the values
// of your arguments
Human(string arg_name, int arg_age) {
name = arg_name;
age = arg_age;
}
void display() {
cout << "Hi I am " << name << " and I am " << age << " years old"
<< endl;
}
// the data members will go out of scope automatically
// since we haven't used new anywhere
~Human() {
cout << "Destructor, but the default one would be OK too!\n";
}
// prompt the user to giving values for name and age
void input() {
cout << "Please input name: ";
cin >> name;
cout << "Please input age: ";
cin >> age;
}
};
int main() {
// Let the user initialize the human
Human human_obj_i;
human_obj_i.input();
human_obj_i.display();
cout << "Now I am going to auto-initialize a human\n";
// Let the program itseld initialize the human
Human human_obj("Samaras", 23);
human_obj.display();
return 0;
}
Example run:
Please input name: Foo
Please input age: 4
Hi I am Foo and I am 4 years old
Now I am going to auto-initialize a human
Hi I am Samaras and I am 23 years old
Destructor, but the default one would be OK too!
Destructor, but the default one would be OK too!
Hope that helps! ;)