in the following code for swapping by call by refernce is used
#include <iostream>
#include <conio.h>
using namespace std;
void swap(int& c,int& d)
{
int temp;
temp=c;
c=d;
d=temp;
}
int main()
{
int a,b;
cout<<" Enter value of a";
cin>>a;
cout<<"\n Enter value of b";
cin>>b;
swap(a,b);
cout<<"a: "<<a;
cout<<"\n b:"<<b;
return 0;
}
but if i use class and make object then there is no need of '&' refernce in calling the method.
#include <iostream>
#include <conio.h>
using namespace std;
class swapy
{
public:
int a;
int b;
void swap(int c,int d)
{
int temp;
temp=c;
c=d;
d=temp;
}
};
int main()
{
swapy s;
cout<<" Enter value of a";
cin>>s.a;
cout<<"\n Enter value of b";
cin>>s.b;
swap(s.a,s.b);
cout<<"a: "<<s.a;
cout<<"\n b:"<<s.b;
return 0;
}
Can you explain how this worked?Why & was not needed in the second program.
First of all you are not calling your class swap() function, because you are not calling it with your class object s, for class swap() function you have to call it with s. like
s.swap(c,d);
Further Change your function swap() name to any other name like mySwap() then you'll get to know that this is not correct, because swap() is a built-in function of std::
so it will be simple to understand if you change the name of your function.
Furthermore, you are not using class member variables correctly, you have to do something with your class member variables a and b. your class is useless here.
Related
I am typing the following code and I am getting the following error at line-1
[Error] no matching function for call to 'int_adder::add()
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.add(); //LINE-1
cout << ia.add(10, 20); //LINE-2
return 0;
}
As pointed by others in the comment, I have corrected it:-
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.adder::add(); //LINE-1
cout << ia.add(10, 20); //LINE-2
return 0;
}
The statement adder::add() will overide the function add() present in int_adder.
Can't fing exact dupe, but you can make overloads from base class visible by using using directive, example:
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
using adder::add; // expose base class overload as our own
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.add(); //LINE-1
ia.adder::add(); // explicit name also works
cout << ia.add(10, 20); //LINE-2
return 0;
}
As the other answer mentions using base class name scope also works. It all depends on your needs and class design.
Basically defining an overload in a derived class prevents implicit method look up from matching base class overloads, so you have to be explicit about it in one way or another.
You have totally two different versions of add in the first place. You are not overriding, you are overloading. You are just providing a new add function that has nothing to do with the other add function of the parent.
So first of all, do you want to override or overload?
Overriding the parent add would like the following:
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add() override {cout <<"int_adder ::add() ";};
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.adder::add(); //LINE-1 <- would work displays adder::add()'s message
cout << ia.add(10, 20); //LINE-2
adder ia = int_adder{}; // now this is interesting
ia.add(); // would work displays adder::add()'s message - cause you did not ask for virtuality
return 0;
}
#include <iostream>
using namespace std;
int display(int a)
{
cout<<"enter a"<<endl;
cin>>a;
cout<<a<<" "<<a<<endl;
}
int display_three_times(int a)
{
cout<<a<<endl;
cout<<a<<endl;
cout<<a<<endl;
}
int main()
{
float a;
display(a);
display_three_times(a);
return 0;
}
By executing the program, I got something like this:
Results of the program.
So my question is, how to use the variables in the second subprogram while they were entered in the first subprogram? I know it's a really basic question, but I do need help :(
You need to pass the result of "display" into "display_three_times". You can do this by replacing
display(a);
display_three_times(a);
with
display_three_times(display(0));
and append return a; to the display function.
Some style tips. Why declare a as a float in main when you are only dealing with int? Since display doesn't take any input you could remove the parameter int a and declare it as a variable instead. display is perhaps misleadingly named since it also reads an integer. To avoid undefined behaviour also return an int from display_three_times or replace it with void; since we do not use the return value of display_three_times we may as well just change the return type to void. With these changes your program looks like:
#include <iostream>
using namespace std;
int read_and_display()
{
int a;
cout<<"enter a"<<endl;
cin>>a;
cout<<a<<" "<<a<<endl;
return a;
}
void display_three_times(int a)
{
cout<<a<<endl;
cout<<a<<endl;
cout<<a<<endl;
}
int main()
{
int a=read_and_display();
display_three_times(a);
return 0;
}
You can use pass by reference or you can return the value from the first function and pass it to another function
Simply passing the variable, means you are passing its value(here 0) only. On passing the variable a in display() you are passing its value only. And inside display() function it is other independent variable 'a' which receives the value. And any change is will be in valid in its scope only inside the block of that function.
To use the same variable in another block you can pass the variable as a refrence variable.
And you can not type cast as int when you are passing it as refrence variable, because it means you are passing the exactly same variable.
#include <iostream>
using namespace std;
int display(float &a)
{
cout<<"enter a"<<endl;
cin>>a;
cout<<a<<" "<<a<<endl;
return 0;
}
int display_three_times(float &a)
{
cout<<a<<endl;
cout<<a<<endl;
cout<<a<<endl;
return 0;
}
int main()
{
float a;
display(a);
display_three_times(a);
return 0;
}
I am trying to pass a variable from one function to another. I have tried this approach but it is not working for me:
int c (){
int x1,x2,y2,y1;
system("cls");
cout<<"Insert Value"<<endl
cin>>x1;
return x1;
}
int cd()
{
int a;
a=c();
cout<<"X1: "<<a;
}
Any help is appreciated. Thanks!
There are a few problems with your code.
First of all you are missing a semicolon after the cout statement in your c() function.
Also you have indicated that function cd() should return an int but you are not returning anything.
Lastly, these function will not begin execution unless you call them explicitly.
Try this:
#include <iostream>
using namespace std;
int c (){
int x1,x2,y2,y1;
cout<<"Insert Value"<<endl;
cin>>x1;
return x1;
}
int cd(){
int a;
a=c();
cout<<"X1: "<<a;
return a;
}
int main()
{
int x=cd(); //call the function to create the side effects
return 0;
}
I just wrote a simple program that has two functions in a class. Problem is when I called them from main(), only the first function executes and the program terminates without calling the second function.
#include <stdio.h>
#include <iostream>
using namespace std;
class exp{
public:
string name;
public:
string fun1(){
cout<<"please enter value for first function ";
cin>>name;
cout<<"yourname from first function is ";
cout<<name;
return 0;
}
string fun2(){
cout<<"Please enter value for second function ";
cin>>name;
cout<<"yourname from second function is ";
cout<<name;
return 0;
}
};
int main(){
exp b1,b2;
cout << b2.fun1();
cout << b1.fun2();
}
The output is
please enter value for first function preet
yourname from first function is preet
You are returning 0 while the return type is string. Constructing a std::string from a null pointer is not allowed. You could use return ""; instead.
Here, try this
#include <stdio.h>
#include<iostream>
using namespace std;
class exp
{
private: // changed from public to private
string name;
public:
int fun1() // changed from string to int
{
cout<<"\nplease enter value for first function ";
cin>>name;
cout<<"\nyourname from first function is ";cout<<name<<endl;
return 0;
}
int fun2() // changed from string to int
{
cout<<"\nPlease enter value for second function ";
cin>>name;
cout<<"\nyourname from second function is ";
cout<<name<<endl;
return 0;
}
};
int main()
{
exp b1,b2;
b2.fun1(); // removed cout
b1.fun2(); // removed cout
}
The problem was you were using cout inside your functions, yet you were also calling them inside a function, that is cout<<b2.fun1();. This is not a good practice.
There was also the problem that the type of your functions were string, but they were returning an integer.
You had also made name as public which just defies the use of OOP. So I made it private.
Cheers......Hope this solves your Problems.. :)
I am unable to compile the following files.
I am trying to pass the name and age to an object and after checking and assigning each age to proper category(adult, kid...) then i am trying to print it.
My 3 files are following:
The first 1:
//cannot access private member declared in class Person
//No constructor could take the source type, or constructor overload resolution was ambiguous.
#include <iostream>
#include <string>
using namespace std;
#include "person2.h"
void getData(Person&);
void displayData(Person&);
int main(){
Person p;
getData(p);
displayData(p);
}
void getData(Person& p){
cout<< "Enter the name: ";
cin>> p.name;
cout<<"Enter the age: ";
int age;
cin>> age;
p.setAge(age);
p.ageGroup = p.determineAgeGroup(age);
}
void displayData(Person& p){
cout<<p.name<< " is in the group of " << p.ageGroup <<endl;
}
The second one:
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
string ageGroup;
void setAge(int&);
string getAge();
string getAgeGroup(int);
private:
int age;
string determineAgeGroup(int );
};
The third one:
#include <iostream>
#include <string>
#include "person2.h"
using namespace std;
void Person::setAge(int& a){
if(a<0) cout<< "No";
}
string Person::getAge(){
return age;
}
string Person::determineAgeGroup(int a){
if(a>= 65) return "Senior";
else if(a<65 & a>= 20) return "Adult";
else if(a<20 & a>= 13) return "Teen";
else return "Kid";
}
string Person::getAgeGroup(int a){
return determineAgeGroup(a);<<endl;
}
Check the following:
endl instead of end in displayData
declare int age in getData and pass that value to p.setAge
return value from getAge should be an int
make determineAgeGroup() public or call determineAgeGroup() from a member function like setAge() or call your public function getAgeGroup().
This should at least get you compiling...
Note: The final edit that solved the problem was bullet point number 4, in particular calling the public function getAgeGroup().