hello ppl i am a new member of the programming club it been a week since i begin to program and i am doing it persnaly so i am sorry if what i posted is stupid but i want u u guys to have a look and tell me whats wrong with this code the question is as under

Q Write a definition of class named Phone that has three private elements country code, the
city code and the number.
• Write two constructors, a default constructor (that initialize each data element of
object with zero) and a constructor that takes three parameters (country code, city
code and the number) and initialize the data member of the object with these
parameters.
• Write a function void printPhone() that displays the data elements of the object.
• Write a function void setPhone(int, int, int) that takes three parameters (country
code, city code and the number) and initialize the data member of the object with
these parameters.
Write a main function create two object of class Phone, the data member of one object is
initialized with zero through default constructor. The data member of second object is
initialized with some values using a constructor that takes three parameters.
Prompt the user to input values (country code, city code and a number) in a main
function, assign these values to the first object (using function setPhone) and display the
value of the data members of two objects using function printPhone().

ans====

#include<iostream.h>
#include<conio.h>

class phone
{
int country,city,no;
public:
       phone():country(0),city(0),no(0)
       {}
       phone(int a,int b,int c):country(a),city(b),no(c)
      {
                 void setphone(a,b,c)
                 { cout<<"enter the codes count city no";
                 cin>>a>>b>>c;
                              } };
                 main()
                 {
                 phone p1,p2(a,b,c);

                 p1.setphone(intx,inty,intz);

                 p1.printphone();
                 p2.printphone() ;    


                      }

Edited 3 Years Ago by Nick Evan: Fixed formatting

Welcome to DaniWeb

First, it does help to put the code in code formatting, like this:

[code]

your code goes here

[/code]

Second, please try to be more specific in your question - such as what incorrect result do you get (and what should the correct result be), what error message do you get on trying to compile, etc. This will help us help you better.

You should read through the sticky messages at the beginning of the forum, many questions will be answered there already.

On to yours.

phone(int a,int b,int c):country(a),city(b),no(c)
{
void setphone(a,b,c)
{ cout<<"enter the codes count city no";
cin>>a>>b>>c;
} };

Two things here. First, you cannot define a function within a function, that's basic C/C++. So the setphone( ) needs to come out. You can make use of it in the phone( ) constructor, if you want.

Second, the setphone( ) should not do any input or output. It takes the parameters given and assigns them to the class's data members. No more, no less. The input/output you do should occur within whatever function or program that makes use of your class.

printphone( ), on the other hand, is meant to do output. It should do just the minimum needed to display the phone number, formatted however you do so in your country. It should not do an endl or have a newline escape code in it, leave that to the user of the function.

I hope you are using an up to date compiler. If so, the basic shell of your code should be updated like this:

#include <iostream>
using namespace std;

//class definitions

int main( )
{
  //program code
  
  return 0;
}

Use the current standards.

First you would have have declared and given values to the arguments a, b, c. As is, you will get compiler error for using identifiers that don't exist. Same with the intx, inty, intz.

vmanes sir one more thing did i used the constructor for the p2 correctly ?

If you are asking about the declaration of your objects to your class as such

phone p1,p2(a,b,c);

Then yes, you can give values of a, b, c with this constructor. Problem occurs when compiling, "What are the values of a, b, c, and are they integer values, character values, what is the data type you are looking for?".

You need to state what the data type is and how you will get those values into the constructor when the object is being initialized.

A. Already have inputted variables, such as

phone p1,p2(1, 2, 3);

B. Have the main program require the user to input variables,

int a, b, c;
cout << "input numbers" << endl;
cin >> a >> b >> c; 
phone p2(a,b,c);

This is basically a rephrase of what Vmanes was suggesting. You will also need to edit what Vmanes had suggested about the structure of your class definition. If you can copy and paste what you have now and any errors you can't figure out, we may be able to help you more along.

Good luck with your programming club.

This question has already been answered. Start a new discussion instead.