Could not separate implementation and interface - c++

I am unable to compile the following files.
I am trying to pass the name and age to an object and after checking and assigning each age to proper category(adult, kid...) then i am trying to print it.
My 3 files are following:
The first 1:
//cannot access private member declared in class Person
//No constructor could take the source type, or constructor overload resolution was ambiguous.
#include <iostream>
#include <string>
using namespace std;
#include "person2.h"
void getData(Person&);
void displayData(Person&);
int main(){
Person p;
getData(p);
displayData(p);
}
void getData(Person& p){
cout<< "Enter the name: ";
cin>> p.name;
cout<<"Enter the age: ";
int age;
cin>> age;
p.setAge(age);
p.ageGroup = p.determineAgeGroup(age);
}
void displayData(Person& p){
cout<<p.name<< " is in the group of " << p.ageGroup <<endl;
}
The second one:
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
string ageGroup;
void setAge(int&);
string getAge();
string getAgeGroup(int);
private:
int age;
string determineAgeGroup(int );
};
The third one:
#include <iostream>
#include <string>
#include "person2.h"
using namespace std;
void Person::setAge(int& a){
if(a<0) cout<< "No";
}
string Person::getAge(){
return age;
}
string Person::determineAgeGroup(int a){
if(a>= 65) return "Senior";
else if(a<65 & a>= 20) return "Adult";
else if(a<20 & a>= 13) return "Teen";
else return "Kid";
}
string Person::getAgeGroup(int a){
return determineAgeGroup(a);<<endl;
}

Check the following:
endl instead of end in displayData
declare int age in getData and pass that value to p.setAge
return value from getAge should be an int
make determineAgeGroup() public or call determineAgeGroup() from a member function like setAge() or call your public function getAgeGroup().
This should at least get you compiling...
Note: The final edit that solved the problem was bullet point number 4, in particular calling the public function getAgeGroup().

Related

C++ class object default constructor bug

I am trying to create a class object s2 with some customized attributes and some attributes from default constructor however my output is the wrong output for the get_year function. It should be outputing 0 which is the key for FRESHMAN but it is out putting 2 instead. The rest of the code is outputting as expected:
#include <stdio.h>
#include <iostream>
#include <algorithm> // for std::find
#include <iterator> // for std::begin, std::end
#include <ctime>
#include <vector>
#include <cctype>
using namespace std;
enum year {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};
struct name
{
string firstName;
string lastName;
friend std::ostream& operator <<(ostream& os, const name& input)
{
os << input.firstName << ' ' << input.lastName << '\n';
return os;
}
};
class Student: name{
private:
name Name;
year Year;
int idNumber;
string Department;
public:
void setname(string fn="", string ln="")
{
Name.firstName =fn;
Name.lastName =ln;
}
name get_name()
{
return Name;
}
void set_year(year yr=FRESHMAN)
{
Year=yr;
}
year get_year()
{
return Year;
}
void set_ID(int ID=0)
{
idNumber=ID;
}
int get_ID()
{
return idNumber;
}
void set_Department(string Dept="")
{
Department=Dept;
}
string get_Department()
{
return Department;
}
};
int main()
{
Student s2;
s2.setname("Nikolai", "Khabeboolin");
s2.set_ID(12436193);
cout<<"ID is: "<< s2.get_ID()<<", name is "<< s2.get_name()<<", year in school is: "<<s2.get_year()<<", Department is "<<s2.get_Department()<<endl;
return 0;
}
Student lacks a constructor, so all its members are default initialized, and the default initialization of year Year and int idNumber is "no initialization", so reading from them is undefined behavior. Reading them might find 0, 2, a random value each time, or crash.
I see that your class contains a void set_year(year yr=FRESHMAN) member, but your code never called set_year, so no part of this executed.
You should make a default constructor for Student, or as Goswin von Brederlow stated, use year Year{FRESHMAN}; and int idNumber{-1}; when declaring the members, to give them default initializations.
By not explicitly declaring and defining a constructor, in this case Student(), you open yourself up to undefined behavior. Your constructor should call set_year(year yr=FRESHMAN) OR even better, just set the year itself.

C String assigning values in explicit constructor in C++?

I have a class BankAccount with two string members - name and num. What I want is to assign values to these objects when I create them (when the constructor is called). However the compiler says No instance of constructor matches the argument list when I try to create an object.
I would like to ask why is that?
// hwk-2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
class BankAccout {
char name[23];
char num[15];
double sum;
public:
BankAccout(char *nm, char *nr, double s) {
strcpy(name,nm);
strcpy(num, nr);
sum = s;
}
};
int main()
{
BankAccout k("Peter", "0403940940", 34.21);
}
as a coffee break exercise here is more idiomatic version
#include "pch.h"
#include <iostream>
#include <string>
class BankAccount {
std::string name_;
std::string num_;
double sum_;
public:
BankAccount(std::string name, std::string num, double sum) {
name_ = name;
num_ = num;
sum_ = sum;
}
};
int main()
{
BankAccount k("Peter", "0403940940", 34.21);
}
The signature of the constructor does not match.
This one will match:
BankAccount(const char *nm, const char *nr, double s);
EDIT:
The reason is the way you are calling the constructor in the main function. You are giving literal strings as parameters. These literals are const, you cannot change them at runtime. Thus you will pass pointers to const char*.
This is very obvious if you look at this opposing example. This is a way that would be compatible with the old signature BankAccout(char *nm, char *nr, double s);.
int main(int argc, char* argv[])
{
char name[] = "hello";
char number[] = "1234";
std::cout << "name before: " << name << std::endl;
BankAccount k(name, number, 8.5);
// name and number are not const,
// you can change them :
name[2] = 'x';
name[3] = 'x';
std::cout << "name after: " << name << std::endl;
return 0;
}
An even simpler version, if you don’t need to have additional functionality in the class: just use a struct.
#include <string>
struct BankAccount {
std::string name;
std::string number;
double balance;
};
int main() {
BankAccount account{"Joy", "44", 43.};
}

Function should have a prototype while declaring in class

I have a string to be printed via a function. I am using turbo-c compiler.
While using procedural method I am able to do it from following code :
#include <iostream.h>
#include <conio.h>
void strr(char name[]);
void main(){
char name1[10];
cout << "Enter name";
cin >> name1;
strr(name1);
getch();
}
void strr(char name[]){
cout << name;
}
But With oop method I am not able to print the string. My Code is :
#include <iostream.h>
#include <conio.h>
class name{
public: void strr(char name[]);
};
void main(){
char name1[10];
cout << "Enter name";
cin >> name1;
strr(name1);
getch();
}
void name::strr(char name[]){
cout << name;
}
With oop method I am getting error Function 'strr' hould have a prototype.
Since your function is defined inside the class, you need an object/instance of the name class to invoke it :
name obj;
cin >> name1;
obj.strr(name1);
Alternatively, if you declare the function as static, then you can invoke it without a class-instance, since the function is a class-function :
class name{
public: static void strr(char name[]) {cout << name << endl;}
};
...
cin >> name1
name::strr(name1);
Try this
name :: void strr(char name[])
{}

Struggling with C++ "was not declared in this scope"

Can anyone tell me why i get the error "name was not declared in the scope when running this?
Thanks.
class lrn11_class{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string lrn11_name;
};
int main()
{
lrn11_class lrn11_nameobject;
lrn11_nameobject.setname("Zee");
cout << lrn11_nameobject.getname() << endl;
return 0;
}
This should work - see comments (BTW use std:: - Why is "using namespace std" considered bad practice?)
#include <iostream>
#include <string>
class lrn11_class{
public:
void setName(const std::string& x){ // Potentially saves copying overhead
name = x;
}
std::string getName() const { // Look up const and its uses
return name;
}
private:
std::string name; // - Used: string lrn11_name; but functions use name!
};
int main()
{
lrn11_class lrn11_nameobject;
lrn11_nameobject.setName("Zee"); // Fixed typo
std::cout << lrn11_nameobject.getName() << std::endl; // Ditto
return 0;
}
You have declare lrn11_name as a member varible for this class. But in set and get functions you are using name.
Other than than you need to call functions as you have defined.
so instead of :-
lrn11_nameobject.setname("Zee");
cout << lrn11_nameobject.getname() << endl;
You have to use following code :-
lrn11_nameobject.setName("Zee");
cout << lrn11_nameobject.getName() << endl;
Make sure that
#include <iostream>
using namespace std;
should be included.

only one function from class intilized in main function of c++

I just wrote a simple program that has two functions in a class. Problem is when I called them from main(), only the first function executes and the program terminates without calling the second function.
#include <stdio.h>
#include <iostream>
using namespace std;
class exp{
public:
string name;
public:
string fun1(){
cout<<"please enter value for first function ";
cin>>name;
cout<<"yourname from first function is ";
cout<<name;
return 0;
}
string fun2(){
cout<<"Please enter value for second function ";
cin>>name;
cout<<"yourname from second function is ";
cout<<name;
return 0;
}
};
int main(){
exp b1,b2;
cout << b2.fun1();
cout << b1.fun2();
}
The output is
please enter value for first function preet
yourname from first function is preet
You are returning 0 while the return type is string. Constructing a std::string from a null pointer is not allowed. You could use return ""; instead.
Here, try this
#include <stdio.h>
#include<iostream>
using namespace std;
class exp
{
private: // changed from public to private
string name;
public:
int fun1() // changed from string to int
{
cout<<"\nplease enter value for first function ";
cin>>name;
cout<<"\nyourname from first function is ";cout<<name<<endl;
return 0;
}
int fun2() // changed from string to int
{
cout<<"\nPlease enter value for second function ";
cin>>name;
cout<<"\nyourname from second function is ";
cout<<name<<endl;
return 0;
}
};
int main()
{
exp b1,b2;
b2.fun1(); // removed cout
b1.fun2(); // removed cout
}
The problem was you were using cout inside your functions, yet you were also calling them inside a function, that is cout<<b2.fun1();. This is not a good practice.
There was also the problem that the type of your functions were string, but they were returning an integer.
You had also made name as public which just defies the use of OOP. So I made it private.
Cheers......Hope this solves your Problems.. :)