Click here to Skip to main content
15,904,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a simple code to understand classes, the code is working good unless i call getlastname function.. if this function have nothing to do then why we write this getfirsttname and getlastname functions .. ???
here is my code please have eye ..
i want to call the get last name function,, but this will generate errors ..
i really need your comments here ..

Thank you For Your Consideration .. !!!!

C++
#include <iostream>
#include <string>
using namespace std;
class personType
{
public:
	void print() const;
	void setName(string first, string last);
	string getFirstName() const;
	string getLastName() ; //  const is removed from here. !!
	personType(string first, string last);
	personType();
private:
	string firstName, lastName;
};
void personType::print() const
{
	cout << firstName << " " << lastName;
}

void personType::setName(string first, string last)
{
	firstName = first;
	lastName = last;
}

string personType::getFirstName() const
{
	return firstName;
}

string personType::getLastName() 
{
	return lastName;
}

// Here Comes The Constructor Turn
personType::personType(string first, string last)
{
	firstName = first;
	lastName = last;
}
int main()
{
	personType p;
	p.setName("Nerdy", "Usman");
	p.print();
	cout << p.getLastName();	// What The heck. ?!
	system("pause");
	return 0;
}
Posted
Comments
thanh_bkhn 4-Jul-13 3:55am    
What is the error message?

1 solution

You simply forgot to define (implement) the costructor without arguments (compiler does not automatically provide the default constructor if you have defined at least one).
Add, for instance
C++
personType::personType()
{
}
 
Share this answer
 
Comments
Usman Hunjra 4-Jul-13 4:08am    
OhHh .. I think I'm over tensed .. !!
its' working thanks .. !!!
CPallini 4-Jul-13 4:11am    
You are welcome.
H.Brydon 4-Jul-13 10:55am    
+5 from me - not sure why somebody downvoted you.
CPallini 4-Jul-13 11:33am    
Thank you very much. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900