there are two scripts, first, square.h second square.cpp
this test program is trying to calculate square and cube of the given number
but i have a problem with cube function definition.

#include <iostream>
#include <conio.h>
using namespace std;

class square {
    int x;
  public:
    void set_values (int);
    int erek () {return (x*x);}
    void set_values_3 (int);
    int erek_3() {return (x*x*x);}
};
void square::set_values (int a) {
  x = a;
void square::set_values_3 (int a) {
  x = a;
}
#include <iostream>
#include <conio.h>
#include "square.h"
using namespace std;
  
int main () {
    int number;
cout <<"enter a number=\n";
cin >> number;
  square burcin;
  burcin.set_values (number);
  burcin.set_values_3 (number);
  cout << "square of "<<number <<" is: " << burcin.erek();
  cout << "cubes of "<<number <<" is: " << burcin.erek_3();
  getch();
}

Recommended Answers

All 7 Replies

What is the problem you are having?

in square.h -- lines 13-17. You can't put executable code in header files like that. If the intent is to make them inline, then do it like you did on line 9, what is void set_values(int a) {x = a;}; square.cpp: delete conio.h and replace getch() with cin.get(). No point using non-portable C code in a c++ program when its not necessary. It does nothing more than make your code look bad to other people (like your instructor).

I am not expert like you. I am learning just less than 1.5months whether you believe or not it is up to you. nevertheless. your writing is not positive. this means that I will not ask any question to this site anymore. but you said that line 13-17 you can not put executable code .. are you sure ? try this. square was working the problem is related with cube.

#include <iostream>
#include <conio.h>
#include "square.h"
using namespace std;
  
int main () {
    int number;
cout <<"enter a number=\n";
cin >> number;
  square burcin;
  burcin.set_values (number);

  cout << "square of "<<number <<" is: " << burcin.erek();

  getch();
}
#include <iostream>
#include <conio.h>
using namespace std;

class square {
    int x;
  public:
    void set_values (int);
    int erek () {return (x*x);}

};
void square::set_values (int a) {
  x = a;

}

above code was compiled. there is not any mistake. you dont get anything by humiliating ..

i use getch() for freezing the screen to see the result. just way of writing.

>>I am not expert like you. I am learning just less than 1.5months whether you believe or not it is up to you.
I made no mention of that, so that statement is not relevant to the problem or the solution. Sorry that you thought my post was not positive -- I wrote nothing that was ciritial of you. I was just trying to help you improve your program and point out problems. If you can't take the heat then get out of the kitchen because you will always find people who critique your programs. If you can't take that then stop programming and take up basket weaving.


>>you can not put executable code .. are you sure ?
Yes, I am absolutely sure, afterall I've been at this enough to know (about 25 years now). If you split up your program so that it uses two or more *.cpp files and put that header file in each one, then the compiler will complain about duplicate functions. Learn to do it correctly right now so that you do it right when the time comes for you to write more complex programs.

>>i use getch() for freezing the screen to see the result. just way of writing
Yes I know -- but what I said doesn't change that. cin.get() will normally do the same thing but in c++ ways. Both cin.get() and getch() will fail to do what you want if there is already one or more keys in the keyboard buffer. So the correct way to do it is to first flush the keyboard buffer of all extraneous keys. There is a sticky thread here in c++ forum about how to flush the keyboard buffer, but it is a bit complex and you may or may not yet have enough c++ knowledge to understand it. So for now just use cin.get().

hi
the problem is that you have missing brace in your head file after line 14 just add it . it will work

thank you alaasam

anytime

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.