Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am newbie to C++. Learning constructors. Please refer to two codes mentioned below, and provide reason, why Code 2 is not working. Thanks.
Code 1:
#include <iostream>
using namespace std;
class Box
{
int x;
public:
Box::Box(int a=0)
{
x = a;
}
void print();
};
void Box::print()
{
cout << "x=" << x << endl;
}
int main()
{
Box x(100);
x.print();
}
Code 2:
#include <iostream>
using namespace std;
class Box
{
int x;
public:
Box(int a=0);
void print();
};
Box::Box(int a=0)
{
x = a;
}
void Box::print()
{
cout << "x=" << x << endl;
}
int main()
{
Box x(100);
x.print();
}
Why the code 1 is working but Code 2 is NOT working?
For some odd reasons you are not allowed to repeat the default value for a parameter:
class Box
{
int x;
public:
Box(int a=0);
//------------^ given here
void print();
};
Box::Box(int a=0)
//------------^^ must not be repeated (even if same value)
{
x = a;
}
Related
This question already has answers here:
How to print class object using operator<<
(3 answers)
Closed 3 months ago.
#include <iostream>
using namespace std;
class PrintName{
public:
void studentName(){
cout<<"Name : "<<studentName<<endl;
}
};
class MathClass{
public:
void multiplicationFunc(int x, int y){
cout<<"Result : "<<(x*y)<<endl;
}
};
int main()
{
cout<<endl;
PrintName PN;
PN.studentName("Mark", "Santo");
MathClass MC;
MC.multiplicationFunc(10,5);
}
I am new to C++ and am learning about classes and objects. From what I gathered classes are ways to group functions and objects is the ability to access them? I am having trouble getting this code to work, I receive an error on line 15 for 'error: no match for 'operator<<'. I am trying to fix my class in order for the main function to work. The output should simply be
'Name : Mark Santo'
'Result : 50'
Thank you for your help everyone!
It seems like there is no error on line 15. Instead, you did not define the variable studentName in your void studentName() function in line 8. Also, you used 2 arguments for studentName() in line 23, where the original function is not taking any. This is the corrected code:
#include <iostream>
using namespace std;
class PrintName{
public:
void studentName(string x, string y){
cout<<"Name: " << x << " " << y << endl;
}
};
class MathClass{
public:
void multiplicationFunc(int x, int y){
cout<<"Result: " << x*y << endl;
}
};
int main()
{
cout << endl;
PrintName PN;
PN.studentName("Mark", "Santo");
MathClass MC;
MC.multiplicationFunc(10,5);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am learning inheritance in C++ and trying to return value from a function "age". All I get back is 0. I have spent hours to figure out, but no luck. This is my code below. I'll greatly appreciate any help on this!
.h class
#include <stdio.h>
#include <string>
using namespace std;
class Mother
{
public:
Mother();
Mother(double h);
void setH(double h);
double getH();
//--message handler
void print();
void sayName();
double age(double a);
private:
double ag;
};
.cpp
#include <iostream>
#include <string>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
Mother::Mother(){}
Mother::Mother(double h)
{
ag=h;
}
void setH(double h){}
double getH();
void Mother::print(){
cout<<"I am " <<ag <<endl;
}
void Mother::sayName(){
cout<<"I am Sandy" <<endl;
}
double Mother::age(double a)
{
return a;
}
main
#include <iostream>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
Mother mom;
mom.sayName();
mom.age(40);
mom.print();
//Daughter d;
//d.sayName();
return 0;
Your mom.print() does this:
cout<<"I am " <<ag <<endl;
So the problem here is: ag = 0
Your mom.age(40) does this:
return a;
See, it doesn't save your mom's age to your mom variable, it only return what you pass ( here is 40 ), so how could it print ?
Therefore, there are many ways to solve this, if you want to return your mom's age, do cout << mom.age(40) in main()
Or, just:
void Mother::age(double a)
{
ag = a;
}
function age doesn't assign value a to the member ag but instead it returns the value a which it takes as parameter it's such a bad thing.
to get what I want to say in main write:
cout << mom.age(40) << endl; // 40
to make it correct change you function age to:
double Mother::age(double a)
{
ag = a;
return a; // it's redundant to do so. change this function to return void as long as we don't need the return value
}
*** another thing you should do:
make "getters" const to prevent changing member data and only let "setters" not constant. eg in your code: class mother:
double getH()const; // instead of double getH() const prevents changing member data "ag"
You have to use correctly setter and getter. Use the setter to change the age like this :
void setAge(const double &_age) {
ag = _age;
}
If you want to retrieve the value use the getter.
double getAge() const {
return ag;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have check and recheck the following code, I don't see what the compiler is talking about. I am a novice with c++, so please be detailed!
the code is:
//myheader.h:
#ifndef MYHEADER_H
#define MYHEADER_H
class myHeader
{
public:
//mutators
void setWidth(int);
void setLength(int);
//accessors
int getWidth();
int getLength();
myHeader();
virtual ~myHeader();
private:
int width,
length;
};
#endif // MYHEADER_H
myheader cpp looks like this:
#include "myHeader.h"
myHeader::myHeader()
{
//ctor
/* void setWidth(int w);
void setLength(int l);
int getWidth();
int getLength();
*/
}
myHeader::~myHeader()
{
//dtor
}
//setWidth will assign a value to the private member width
void myHeader::setWidth(int w)
{
width = w;
}
//setLength will assign a value to the private member Length
void myHeader::setLength(int l)
{
length = l;
}
//getWidth will return the value for the width member
int myHeader::getWidth()
{
return width;
}
//getLength will return the value for the length member
int myHeader::getLength()
{
return length;
}
the main is not yet finished, and looks like this:
#include "myHeader.h"
#include <iostream>
using namespace std;
int main()
{
myHeader impleHeader;
int locLength = 0;
int locWidth = 0;
cout<<"width / length";
cin>>locLength>>locWidth;
myHeader.setLength(locLength);
myHeader.setWidth(locWidth);
return 0;
}
I don't see any problem, but a trained eye can definitely spot it. please tell me what am I doing wrong
myHeader is the name of a type, not an object.
You wanted to invoke setLength() and setWidth() on the object impleHeader:
impleHeader.setLength(locLength);
impleHeader.setWidth(locWidth);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to allocate a variable within the scope of a parameter list? By using new we can do the following :
Class A{ /*... snip ...*/ };
void myFunc(A* a){ }
int main(...){
myFunc(new A());
return 0;
}
This will create a new A. What if the signature of myFunc was
void myFunc(A a);
instead. Is there a syntax to create local instance inside the myFunc() parameter list? I'm looking for something like
myFunc(A());
or
myFunc(A a());
Another use would be for something like :
A a(123);
if(a == A(123)){ }
The net effect is to save one line, but it also creates a scope within the parameters list which makes me wonder if it is allowed at all.
If you just want to create a variable to pass to the function you can use a aggregate initialization / list initialization
#include <iostream>
#include <cmath>
using namespace std;
class A{ /*... snip ...*/ };
void myFunc(A a){ }
int main(){
myFunc(A{});
return 0;
}
Live Example
You can also use this with classes that have constructors that take multiple parameters
#include <iostream>
#include <cmath>
using namespace std;
class A
{
private:
int foo;
int bar;
double foobar;
public:
A(int a, int b, double c) : foo(a), bar(b), foobar(c) {}
};
void myFunc(A a){ }
int main(){
myFunc(A{1,2,3.0});
return 0;
}
Live Example
C++ supports this with the myFunc(A()); syntax you posed in your question.
#include <stdio.h>
char lazybuff[500];
class Point
{
public:
Point (double x, double y) : m_x(x), m_y(y) { }
char * ToString (void) { sprintf (lazybuff, "%f, %f", m_x, m_y); return lazybuff; }
private:
double m_x, m_y;
};
void print_point (Point print_me)
{
printf ("%s\n", print_me.ToString());
}
int main (void)
{
print_point (Point(5, 3));
return 0;
}
This question already has answers here:
What are access specifiers? Should I inherit with private, protected or public?
(2 answers)
Closed 9 years ago.
How do you guys find the value of s.x from this code
I am beginner of c++ and dont know how to solve it
Please help thanks
// StarterLab.c : C Program to convert to C++
//
//#include "stdafx.h" // required for Visual Studio
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//#include "MemTracker.h"
#pragma warning (disable:4996)
using namespace std;
struct variable
{
friend void showCalculation(variable a);
private:
int x;
int y;
int sum;
public:
void Calculate(int x,int y);
};
void showCalculation(variable a)
{
printf("%d",a.sum);
};
void variable:: Calculate (int x,int y)
{
sum = x + y;
};
int main ()
{
variable s;
s.Calculate(7, 6);
showCalculation(s);
printf("%d",s.x);
}
How do you guys find the value of s.x from this code
I am beginner of c++ and dont know how to solve it
Please help thanks
The variable x is private, so you cannot access it directly. You could add a member function to get it:
int variable::GetX() {
return x;
}
printf("%d", s.GetX());
You can not access s.x because x is a private member. You have two options.
Create a getter:
int variable::X() { return x; }
or make it public:
public:
int x;
int y;
int sum;
Note that using getters/setters is the appropriate way of doing this.