954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Deitel's "C++ How To Program" exercise 2.18

what about this question.........what is a default comstructor? how are an object's data members initialized if a class has only an implicitly defied default constructor? explain the purpose of data member.

<< moderator edit: split thread (was a reply to [thread=15829]this[/thread]) >>

mugilan
Light Poster
33 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

A default constructor is a constructor that takes no arguments or fully defaulted arguments. The values of the members of your object are set to a bitwise zero unless otherwise specified by default values. The implicit default constructor just zeros all your members.Its purpose is mainly to ensure your objects are created initialised to a certain value rather than containing whatever garbage was previously in those memory locations.

Stoned_coder
Junior Poster
164 posts since Jul 2005
Reputation Points: 19
Solved Threads: 5
 

explain the difference between a function prototype and function definition.....what about this question......mate

mugilan
Light Poster
33 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

a function prototype introduces the function to the compiler. It tells the compiler about what arguments the function takes and what it returns. A prototype looks like this...

return_type func_name( arguments );
// i.e.
int func(char a,int b,long c);

Now a definition also provides code and takes the following structure...

return_type func_name( arguments )
{
   // code here
}

// i.e.

int func(char a,int b,long c)
{
    if (a<c) 
       return b;
   else
      return 0;
}
Stoned_coder
Junior Poster
164 posts since Jul 2005
Reputation Points: 19
Solved Threads: 5
 

this is the last one.......


(employee class)create a class called employee that include three pieces of information as data members - a first name(tye string), a last name(type string) and a monthly salary(type int). your class should have a constructor that initializes the three data members. provide a set and a get function for each data member. if the monthly salary is not positive, set it to 0. write a test program that demostrates class employee's capabilities. create two employee objects and display each object's yearly salary. then give each employee a 10 percent raise and display each employee's yearly salary again

mugilan
Light Poster
33 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

If you write each statement on its own line, you can see that it spells out much of the initial code for you. Make an initial attempt and post that.
create a class called employee that include three pieces of information as data members[indent] - a first name(tye string),
a last name(type string)
and a monthly salary(type int).
your class should have a constructor that initializes the three data members.
provide a set and a get function for each data member. [indent]if the monthly salary is not positive,[indent]set it to 0. [/indent][/indent][/indent]write a test program that demostrates class employee's capabilities.
create two employee objects and
display each object's yearly salary.
then give each employee a 10 percent raise and display each employee's yearly salary again

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Originally Posted by mugilan
create a class called employee that include three pieces of information as data members
- a first name(tye string),
a last name(type string)
and a monthly salary(type int).
your class should have a constructor that initializes the three data members.
provide a set and a get function for each data member.
if the monthly salary is not positive,
set it to 0.
write a test program that demostrates class employee's capabilities.
create two employee objects and
display each object's yearly salary.
then give each employee a 10 percent raise and display each employee's yearly salary again

mugilan
Light Poster
33 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 
Make an initial attempt

...at code...and post that. :rolleyes:

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
#include<iostream>

using namespace std;

class employee
{
    declare name as a string;
     last name as a string;
     monthly salary as a int;

public:
        void getdata(void);
        void setdata(void);
  
       emlployee()
      {
      monthly salary = 0
      last name = arun;
     first name = arun;
};
     void employee::getdata(void)
{
   cout<<"a first name:";
   cin>>name
  cout<<"a last name:";
   cin>>name
  cout<<"monthly salary:";
}
  void employee::setdata(void)
  {
     cout<<"first name";<<first name<<"\n";
     cout<<"last name";<<last name<<"\n";
     cout<<"monthly salary";<<monthly salary<<"\n";
   }  
   return 0;
}


<< moderator edit: added [code][/code] tags >>

can you plese correct it for me

mugilan
Light Poster
33 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

This

declare name as a string;
     last name as a string;
     monthly salary as a int;


should be like this

string first;
     string last;
     int salary;


Spelling is important.

emlployee()
      {
      monthly salary = 0
      last name = arun;
     first name = arun;
      }
};


Good indentation helps. String literals are enclosed in double-quotes.

There's more, like returning a value from a void function or misplaced semicolons, but let's try a little at a time for now.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

still got error.....please correct me

mugilan
Light Poster
33 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Yeah, I figured so.but let's try a little at a time for now.But I'm not clairvoyant, so please post your latest.(Oh, I'm repeating my signature.)

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

<< moderator edit: added code tags : [code][/code] >>

#include<iostream>
using namespace std;

class employee
{
	string first;
	string last;
	int salary;

public:
	void getdata(void);
	void setdata(void);

	employee()
	{
		monthly salary = 0
		last name;
		first name;
	}
};
	void employee::getdata(void)
	{
		cout<<"a first name:";
		cin>>name;
		cout<<"a last name:";
		cin>>name;
		cout<<"monthly salary:";
	}
	void employee::setdata(void)
	{
		cout<<"first name";<<first name<<"\n";
		cout<<"last name";<<last name<<"\n";
		cout<<"monthly salary";<<monthly salary<<"\n";
		return 0;
	}


<< moderator edit: added code tags : [code][/code] >>

why still got error.....

mugilan
Light Poster
33 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 
why still got error.....

can post error?


Variable names likefirst name are not valid because whitespace separates tokens. That's much of the reason us old crusty folks use underscores and write it as first_name. It may look ugly to some, but it keeps the compiler happy. (And it's kinda like I wrote earlier.)

Do you have some introductory text you to work with, or are you strictly learning on the fly with the 'net? A book would help immensely, but we'll do what we can.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

way.cpp(16): error C2065: 'monthly' : undeclared identifier

way.cpp(16): error C2146: syntax error : missing ';' before
identifier 'salary'

way.cpp(17): error C2146: syntax error : missing ';' before identifier 'last'

way.cpp(17): error C2146: syntax error : missing ';' before identifier 'name'

way.cpp(17): error C2065: 'name' : undeclared identifier name


way.cpp(18): error C2146: syntax error : missing ';' before identifier 'name'

way.cpp(18): error C3861: 'name': identifier not found, even with argument-dependent lookupc:

.cpp(24): error C2593: 'operator >>' is ambiguousc:\

.cpp(24): error C3861: 'name': identifier not found, even with argument-dependent lookup

.cpp(26): error C2593: 'operator >>' is ambiguous

way.cpp(26): error C3861: 'name': identifier not found, even with argument-dependent lookup

.cpp(31): error C2143: syntax error : missing ';' before '<<'c:\

.cpp(32): error C2143: syntax error : missing ';' before '<<'

way.cpp(33): error C2143: syntax error : missing ';' before '<<'

.cpp(34): error C2562: 'employee::setdata' : 'void' function returning a value

mugilan
Light Poster
33 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Honestly, are you reading these posts?Variable names like first name are not valid because whitespace separates tokens. That's much of the reason us old crusty folks use underscores and write it as first_name. It may look ugly to some, but it keeps the compiler happy. (And it's kinda like I wrote earlier.)Your subsequent questions were already answered in this thread as well.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

<< moderator edit: added code tags : [code][/code] >>

#include<iostream>
using namespace std;

class employee
{
	string first;
	string last;
	int salary;

public:
	void getdata(void);
	void setdata(void);

	employee()
	{
		monthly salary = 0;
		last_name;
		first_name;
	}
};
int main()
{
	void employee::getdata(void)
	{
		cout<<"a first name:";
		cin>>name;
		cout<<"a last name:";
		cin>>name;
		cout<<"monthly salary:";
	}
	void employee::setdata(void)
	{
		cout<<"first name";<<first name<<"\n";
		cout<<"last name";<<last name<<"\n";
		cout<<"monthly salary";<<monthly salary<<"\n";
	}
	return 0;
}


<< moderator edit: added code tags : [code][/code] >>

but i am still got error....but less error....can you tell where is the problem

mugilan
Light Poster
33 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 
but i am still got error....but less error....can you tell where is the problem

I've been trying.

The answer(s) has(have) already been posted in this thread.

[edit]

int main()
{
	void employee::getdata(void)
	{
		cout<<"a first name:";
		cin>>name;
		cout<<"a last name:";
		cin>>name;
		cout<<"monthly salary:";
	}
	void employee::setdata(void)
	{
		cout<<"first name";<<first name<<"\n";
		cout<<"last name";<<last name<<"\n";
		cout<<"monthly salary";<<monthly salary<<"\n";
	}
	return 0;
}


And the whole business of nested functions is not an improvement.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

brother i want to ask you.......the program that i write is it can use for this question

(employee class) create a class called employee that include three
pieces of information as data members - a first name (type string), a last
name (type string) and a monthly salary (type int).your class should
have a constructor that initializes the three data members. provide a set
and a get function for each data member.if the monthly salaryis not
positive, set it to 0. write a test program that demonstrates class
employee's capabilties. create two employee objects and display each object's
yearly salary.then give each employee a 10 percent rase and display
each employee's yerly salary again.......please teach me how to do it

mugilan
Light Poster
33 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

brother i want to ask you.......the program that i write is it can use for this question

please teach me how to do it

I'm trying to do so without blatantly dumping working source code that I could easily write in obvious violation of the homework policy . Instead, I am trying to encourage you to learn and hopefully accelerate the process, with your help, by pointing to various bits that are most glaringly errors. But a one-sided teaching/learning process benefits neither.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You