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() ;    


                      }

Dani AI

Generated

Short summary and a clean, minimal design based on the replies from , and .

Common mistakes in the original post: a function was defined inside the constructor (invalid in C++), the setter performed input/output instead of simply assigning members, variables were used before being declared when creating objects, old headers like <iostream.h> and conio.h were used, and main() lacked an explicit return type. Also watch case sensitivity: Phone and phone are different identifiers.

A concise class declaration that follows the question requirements:

#include <iostream>
using namespace std;

class Phone {
private:
  int countryCode;
  int cityCode;
  int number;
public:
  Phone();                        // default -> zeros
  Phone(int c, int ci, int n);    // parameterized
  void setPhone(int c, int ci, int n);
  void printPhone() const;
};

Notes on implementation and use

  • Default constructor should initialize members to 0; the parameterized constructor should use an initializer list to assign values.
  • setPhone should only assign the three integers to the members; perform input (cin) in main() or another caller.
  • printPhone should perform output formatting; avoid forcing a newline so callers control layout.
  • When creating objects with the parameterized constructor, supply already-declared integers or constants (e.g., after reading input into a,b,c, call Phone p2(a,b,c)).

Troubleshooting tips

  • "identifier not found" typically means variables were not declared before use.
  • "function defined here" style errors often come from misplaced braces (check that methods are declared in class scope, not inside other functions).
  • Compile with a modern compiler and warnings enabled (for example, g++ with -std=c++11 -Wall) to get clear diagnostics.

As noted by and , separate class logic from input/output and format code using code blocks as suggested. later confirmed the corrected approach worked.

Recommended Answers

All 6 Replies

use the CODE blocks to demark your code, so its simpler to read

class Hello
{

};

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.

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

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.

thanks guys i finally got it write thanks to all of u

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.