How to use classes in C++? [closed] - c++

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 6 years ago.
Improve this question
I can't see to find the problem in my code. Nor the solution on the internet. I can see that i can create the code in a different way but i need to know how to work with it written like this:
class Triunghi{
int l1=0;//latura1
int l2=0;//latura2
int ba=0;//baza
int p=0;//perimetru
public:
Triunghi(){}
Triunghi(int a):l1(a){}
Triunghi(int a,int b):l1(a),l2(b){}
Triunghi(int a,int b,int c):l1(a),l2(b),ba(c){}
Triunghi(int a,int b,int c,char *msg):l1(a),l2(b),ba(c){
cout<<msg<<""<<l1<<l2<<ba<<endl;
}
Triunghi(Triunghi &A){
l1=A.l1;
l2=A.l2;
ba=A.ba;
}
~Triunghi(){
}
int Perimetru()
{
p=l1+l2+ba;
return (p);
}
};
It works fine there are no errors but i cant seem to give value to l1, l2 and ba, to use them in function 'Perimetru'.
This is how my main looks.
int main()
{
Triunghi tri;
Triunghi(1,2,3);
tri.Perimetru();
return 0;
}
How to make it work?

Triunghi tri;
Triunghi(1,2,3);
This doesn't do what you think it does. tri is created with default constructor, so all of your members are set with their in-class default values, meaning 0. Then, you create temporary object Triunghi, and you don't even use it. What you might wanted to do instead is:
Triunghi tri(1,2,3);
Then Perimetru() returns 6 as expected.

Related

How to do I implement oop with a number of questions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have about 40 questions. (all the answers to the questions are of either int or float data type)
I will display the question and ask the user to input his/her answer.
Then I will check if the answer is correct or not.
I need to implement this program in object oriented programming.
I thought about doing it this way,questions by questions..but is there another way of doing this? Please help me..
The code below is sort of a pseudocode . I use c++ .
class workout{
private:
float variables...;
public:
workout();
workout(float variables...);
void answers();
int Display();
~workout();
};
...
void workout::answers(){
declare variables;
display question1;
input answer1
check if correct
display question2;
input answer2
check if correct
display question3;
..and so on..
}
workout::~workout(){
}
int main(){
...
return 0;
}
Well you could have a Question class, and then go from there. Here's an example.
class Question
{
public:
std::string question
float answer;
Question(std::string _question, float _answer)
: question(_question), answer(_answer) {}
void Display() {}
bool Check(float input) {}
};
And then maybe create an array of questions?

C pointers and objects as part of different classes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have relatively less experience with C++ coding, and I'm working on a project that requires me to use classes, pointers and objects in C++. I'm facing some basic problems while practising coding.
This is what part of my .cpp file looks like:
A::A(int x, int y):CD()
{
//some code
}
B::B(int z):CD()
{
//some code
}
If I need to pass values from A to B, how would I be able to do it in such a scenario? Could anyone please clarify and help me.
Edit: According to the code I have, CD is also defined as a class like A and B.
Here A is a class and B is another class right,
Let's take a simple addition example
A(int x,int y)
{
b=new B(x+y);//let be a reference of class B
//this will call B's constructor
}
B(int z)
{
this.z=z;//lets's assume that z is a variable in B class
}

C++ When comparing, pass class as pointer or normally? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm quite new to C++, and since I want my code to be good, I have a small question, let's say I have a function
UI::GetColor(CClass Class)
{
if (Class.m_Something)
return 0;
return 1;
}
Is it better to pass the CClass as a pointer, so it's not being copied or not? I saw a lot of code using different styles of that and I'm kind of confused which one is better and why. Thanks for answers.
The function should be written as follows:
class UI {
...
int GetColor(const CClass &c) const {
if (c.m_Something) {
return 0;
}
return 1;
}
}
The reference avoids an unnecessary copy; the const-parameter states that the parameter will not be changed; the const-function declarator states that the function will not change the this-object (i.e. the UI-instance on which it is called).

confused about class structure in code [closed]

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 5 years ago.
Improve this question
I am reading a sample code that uses C++ and classes, I am new on C++ classes I can work with basics similar to this http://www.cplusplus.com/doc/tutorial/classes/, but I cant understand what the code below does mean or the color it is using visual studio c++
thanks
I am sorry if it is a fool question
It creates an object named some by instantiating the class some.
Then it calls the member function ToVector() on the object some and pass the result of the call to the function named function.
class is blue because it is a keyword of the C++ language.
The first some is green because it is the name of a class.
The second some is black because it is a variable.
And function and ToVector are red because the are functions.
Now this is ugly code because you "hide" the class some by reusing the same name for your variable. Also you do not need to put the word class here.
Here is a more complete and nicer version:
#include <vector>
class Some
{
public:
std::vector<int> ToVector()
{
return std::vector<int>(); //return an empty vector
}
};
int f(std::vector<int> v)
{
return 0;
}
int main(int, char**)
{
Some some; // Was "class some some"
return f(some.ToVector());
}

representing if in c++ as a function [closed]

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 any way of writing If as a function in c++?
I am not sure if I write my question correct.
int sum(int a, int b)
int total;
total = a + b;
return total;
So this is sum function, how can I write If in that way?
Something like that?
void if_else(bool cond, std::function<void()> if_, std::function<void()> else_) {
cond ? if_() : else_();
}
Even to there is still an if/else statement in the form of the ternary operator...
This one may be another solution:
void if_else(bool cond, std::function<void()> if_, std::function<void()> else_) {
(cond && ([&]() { if_(); return true; })()) || else_();
}
I'm not sure about the second one, mainly because of the definition of a function immediately executed, but I guess the idea is pretty clear.