줄리아 우지야노바
write function from the library unistd.h
Use standard library functions instead of platform dependent
Ercan
i should do this in the given frame
Ercan
with the write function
줄리아 우지야노바
i should do this in the given frame
You need to show integer 128 like "128", not like character encoded with number 128 in ascii, i am right?
AKI David
Please does anyone know how to pass a structure to a Function
chakiwinja
Please does anyone know how to pass a structure to a Function
pass a pointer to it, add the struct keyword before it, or you could typedef to avoid typing "struct" 200 times
chakiwinja
something like: struct box{ int height; int width;} ; void function (struct box* b) { //do stuff with box }
AKI David
Also Please is it Possible to pass a Char type value to an int function?
AKI David
I tried but It keeps showing me an error
AKI David
|38|error: conflicting types for 'get_value'; have 'int(char)'|
Hussein
with the write function
do you want to write integers to the disk?
METRO
windows form application develop?
IPTV E P2P
I made a naval battle game in C for college but I need to put something when the game ends click N to start over how can I do that?
Anonymous
Put the runtime inside of a while loop with no break
then set while condition as if user input equals X then break the loop
A
#include<iostream> using namespace std; class calculator{ float a,b; friend class scificalculator; public: void set(){ cout<<"Enter the two numbers : "; cin >>a>>b; } float add(){ return(a+b); } float sub(){ return(a-b); } float mul(){ return(a*b); } float div(){ return(a/b); } void display(){ cout<<"The sum of numbers entered is : "<<add()<<endl; cout<<"The subtraction of numbers entered is : "<<sub()<<endl; cout<<"The multiplication of numbers entered is : "<<mul()<<endl; cout<<"The divison of numbers entered is : "<<div()<<endl; } }; class scificalculator { void addtfactorial(){ int d=0; for(int i=0;i<(a+b);i++){ d=d+i; } } }; int main(){ calculator c; c.set(); c.display(); return 0; }
klimi
"Can we access private variables with frnd fxn if yes then why this code gives me error a nd bnot defined" please help
you don't have any reference to the calculator in scificalculator and therefore a is not defined
klimi
you can just access "calculator".a or .b
klimi
in class scificalculator { void addtfactorial(){ int d=0; for(int i=0;i<(a+b);i++){ d=d+i; } } }; Is not said what a and b are
A
Thank you 😊😊
klimi
np
A
#include<iostream> using namespace std; class calculator{ float a,b; friend class scificalculator; public: void set(){ cout<<"Enter the two numbers : "; cin >>a>>b; } float add(){ return(a+b); } float sub(){ return(a-b); } float mul(){ return(a*b); } float div(){ return(a/b); } void display(){ cout<<"The sum of numbers entered is : "<<add()<<endl; cout<<"The subtraction of numbers entered is : "<<sub()<<endl; cout<<"The multiplication of numbers entered is : "<<mul()<<endl; cout<<"The divison of numbers entered is : "<<div()<<endl; } }; class scificalculator { void addtfactorial(){ float d=0; float m = calculator.a; float n = calculator.b; for(int i=0;i<(m+n);i++){ d=d+i; } } }; int main(){ calculator c; c.set(); c.display(); return 0; }
A
Still facing the issue
Pavel
Still facing the issue
In addfactorial(), what is calculator?
A
In addfactorial(), what is calculator?
Yea was trying to buid a calculator for placticing classes
Pavel
Yea was trying to buid a calculator for placticing classes
The variable doesn't exist in that context
A
Trying to import its members
Pavel
..
To use a variable you need to declare it, you can't use class name as a variable
A
I am not using class name as a variable i just want to call the variables defined in the above class how shall i do that
A
I was trying with frnd fxn what shall i do here in this code
Pavel
Pavel
Okay can you help me with that
I can try (I'm walking outside though, so it won't be easy to me), or you can google that.
A
Oh instance is the object
A
I know what is that
Pavel
Okay can you help me with that
In short, you have class calculator, it is the description. in your main function you have variable c which is instance of your class. Only instances have real data. Class don't have data, unless that data is static (but that's not what you probably need here). Imagine it as a blueprint of something and the thing you've built ny that blueprint
Pavel
I know what is that
Ok, then to get a and b you need an object
A
I was calling a instance in the other class
A
A and b are not instances ?
Pavel
🤔🤔ok
You can pass your c variable to your function as an argument
A
Ohhh got it
A
You can pass your c variable to your function as an argument
Yea yea i got it i am doing so small mistake
A
Thnx alot its working now
A
I am accessing the variables but there values are comming 0 as i access them why is this so ? @gameraccoon can you help me out here
A
#include<iostream> using namespace std; class calculator{ float a,b; friend class scificalculator; public: void set(){ cout<<"Enter the two numbers : "; cin >>a>>b; } float add(){ return(a+b); } float sub(){ return(a-b); } float mul(){ return(a*b); } float div(){ return(a/b); } void display(){ cout<<"The sum of numbers entered is : "<<add()<<endl; cout<<"The subtraction of numbers entered is : "<<sub()<<endl; cout<<"The multiplication of numbers entered is : "<<mul()<<endl; cout<<"The divison of numbers entered is : "<<div()<<endl; } }; class scificalculator { calculator k; float addtfactorial(){ float d=0; for(float i=0;i<(k.a+k.b);i++){ d=d+i; } return(d); } float addtsqr(){ return((k.a+k.b)*(k.a+k.b)); } float addtcube(){ return((k.a+k.b)*(k.a+k.b)*(k.a+k.b)); } public: void display(){ cout<<k.a<<k.b; cout<<"The addition and factorial of numbers entered is : "<<addtfactorial()<<endl; cout<<"The addition then square of numbers entered is : "<<addtsqr()<<endl; cout<<"The addition then cube of numbers entered is : "<<addtcube()<<endl; } }; int main(){ calculator c; scificalculator l; c.set(); c.display(); l.display(); return 0; }
A
One sec is it due to the reason that its in l object and that was in c object
A
Am i right?
Pavel
One sec is it due to the reason that its in l object and that was in c object
In scificalculator you store another instance of your calculator, you need to set some value to it before use.
Pavel
So in your case l object doesn't know about c object
A
Yea got it👍🏻 thnx 😁
A
Just one more thing
A
Can i use set fxn again in scificalculator without creating it again
A
If yes then how shall i do that🤔
Pavel
A
Void set () fxn in class classroom at top
Pavel
Void set () fxn in class classroom at top
I don't get your question, sorry
A
Like i have made a fxn in class void set() which inputs the variables a and b i will need to call it again to get 2 numbers again in that object l for scifi calculator So shall i make another set fxn in scificalculator Or can i use the set fxn() made in calculator itself and call that fxn in scificalculator
Pavel
Like i have made a fxn in class void set() which inputs the variables a and b i will need to call it again to get 2 numbers again in that object l for scifi calculator So shall i make another set fxn in scificalculator Or can i use the set fxn() made in calculator itself and call that fxn in scificalculator
It depends on what relationship you want to make between your two classes. Is one an extension of the other. Or owner of it, or wrapper around it. Because you can pass a calculator to the constructor to create the scificalculator, you can create a function that will call the function of the calculator, you can make a setter that sets the calculator, you can store a reference to an outside calculator, and use that outside calculator to read the data
Pavel
Probably many more ways how to organize this
A
Those are actually two different classes I don't want to make any relationship btwn them just wanted to use some of the fxns made by the upper class in lower class
A
Can i do that?
Pavel
Those are actually two different classes I don't want to make any relationship btwn them just wanted to use some of the fxns made by the upper class in lower class
One way to do this that I would use, you can make a class/struct with data, e.g. CalculatorData, put a and b there as public fields. Then make free functions that would take a CalculatorData object as an argument. Group these functions into two namespaces, or into two classes as static functions.
Pavel
Then you unbind your classes and can call common functions from each other if needed
A
Ohkk but i cannot directly use them right ?
Pavel
Ohkk but i cannot directly use them right ?
Well, it depends how you want to use them, you probably can if it makes logical sense