imminent
Hi everyone,just a question. I'm taking a course and one of it exercise the instructor explicit ask us to throw a exception in a class constructor.
I write this code, but thinking about performance is that the better way to do that?
#include <cassert>
#include <stdexcept>
class Pyramid {
int length {};
int width {};
int height {};
int ValidateValue(int n){
if(n < 0)
throw std::invalid_argument("invalid number!");
return n;
}
public:
Pyramid(int l, int w, int h) : length(ValidateValue(l)), width(ValidateValue(w)), height(ValidateValue(h)) {
};
void Length(int lng){
length = ValidateValue(lng);
}
int Length(){return length;}
void Width(int wdt){
width = ValidateValue(wdt);
}
int Width(){ return width;}
void Height(int hgt){
height = ValidateValue(hgt);
}
int Height(){ return height;}
int Volume(){
return (width * length * height) /3;
}
};
// Test
int main() {
Pyramid pyramid(4, 5, 6);
assert(pyramid.Length() == 4);
assert(pyramid.Width() == 5);
assert(pyramid.Height() == 6);
assert(pyramid.Volume() == 40);
bool caught{false};
try {
Pyramid invalid(-1, 2, 3);
} catch (...) {
caught = true;
}
assert(caught);
}
seriously that teacher is retarded tho
Dima
very sus
borealis
it is just Exercise for teach of constructor throw Exceptions mechanism.friend please Find updated Code Below for causing Exception to Throw For Invalid Triangle after negative value Issue being resolved by Appropriate unsign integer usage .
Below is check if Mathematical triangle can be form , raise exception if Not possible .
Pyramid(unsigned int l, unsigned int w, unsigned int h) : length(l), width(w), height(h) {
auto sides = { length, width, height };
auto max = *std::max_element(sides.begin(), sides.end());
auto sum = std::accumulate(sides.begin(), sides.end(), 0);
sum > 2 * max ?0: throw std::invalid_argument("invalid triangle!");
};
Dima
lol
borealis
sorry External link,, it was Link for helpful Triangle calculation web site.
Danya🔥
Wtf is going on
Ughhh
Hello. Does anyone have a .net maui todo list application?
Ughhh
That is fully functional
borealis
this is Not DOt net Programming group
Paulo
it is just Exercise for teach of constructor throw Exceptions mechanism.friend please Find updated Code Below for causing Exception to Throw For Invalid Triangle after negative value Issue being resolved by Appropriate unsign integer usage .
Below is check if Mathematical triangle can be form , raise exception if Not possible .
Pyramid(unsigned int l, unsigned int w, unsigned int h) : length(l), width(w), height(h) {
auto sides = { length, width, height };
auto max = *std::max_element(sides.begin(), sides.end());
auto sum = std::accumulate(sides.begin(), sides.end(), 0);
sum > 2 * max ?0: throw std::invalid_argument("invalid triangle!");
};
I understand what ware you doing the exercise, explicit mention throw a exception for invalid number, this exercise also specify the type of values.
And also if you make something like that.
Pyramid pyramid(-4, -4, 6); the constructor will convert de negative number in a bigg positive number, because of the type rules and signal bit.
My question is more about the good practices about validate variables inside a constructor the best way to do that.
I appreciate your help.
web Developer
Please I need you guys to suggest me C++ mini project👌?
Иван
Paulo
Hi everyone I'm read this article and found a good explanation about emplace_back and push_back.
Paulo
This is the article: https://andreasfertig.blog/2023/04/push_back-vs-emplace_back-when-to-use-what/#:~:text=emplace_back%20for%20better%20performance,And%20that%20is%20
What I understood is that emplace back only has performance improvements if it can create the object inside the vector, if you give a temporary object or an object it will behave exactly like push-back
Paulo
Is that correct? I showed this article to an experienced C++ developer and he didn't agree, but the source code shows that is correct.
FriedRice
I think think there is no difference in performance, just semantic difference.
web Developer
Paulo
In some cases if emplace can create an object inside the vector it will avoid operations and only call the constructor of the object
Pavel
Danya🔥
Pavel
Paulo
Object obj;
vector v;
v.push_back(obj);
v.emplace_back(obj);
v.push_back(Object());
v.emplace_back(Object());
In both cases the exists and is just passed for the function and both emplace and push have the same behavior.
Paulo
Want to say thanks, this kind of discussion helps me a lot.
D1ws
https://www.youtube.com/watch?v=7xwjjolDnwg
Danya🔥
https://www.youtube.com/watch?v=7xwjjolDnwg
I watched this talk halfway yesterday and it seemed to me chaotic, as if the speaker was trying to fit a course on operating systems and assembly programming into one presentation. It's not bad, but it can be confusing.
Anonymous
So guys, can anyone help me solve this exercise? Make a program that performs simple operations on complex numbers:
• Create and read two complex numbers z and w, composed of a real part and an im-
gym.
• Present the addition, subtraction and product between z and w, in that order, as well as the
module of both.
Danya🔥
web Developer
web Developer
Who need Grading system source code using pure C++ ?
FriedRice
I mean pushback takes rvalue reference as well so it may somehow construct object in-place imo.
Priyanshu
What is flow chart
Priyanshu
Flow chart
Pavel
I mean pushback takes rvalue reference as well so it may somehow construct object in-place imo.
The object that is going to be stored will be constructed in the corresponding place in the vector, there's no way from that.
push_back will construct it via copy or move constructor.
emplace_back will construct it with a constructor that matches the provided arguments (so if you pass another object of this type it will try to call the copy constructor, and if you pass rvalue reference or temporary it will try to call the move constructor).
Jhinga_Man
How can I learn C programming, specifically after arrays
Jhinga_Man
How can I start DSA?
Manav
How can I start DSA?
Data structures are mostly language independent. It's not like binary trees look much different when coded in C or javascript, for instance.
Jhinga_Man
Jhinga_Man
So can we talk in DMs?
Manav
So can we talk in DMs?
We can talk here, it's more beneficial for you as it also prevents me from talking bs as others can check my replies.
Jhinga_Man
Okay😂 suits me
Danya🔥
Jhinga_Man
Like I am in college.... So from where should I start in C language?
Jhinga_Man
I have learned loops, conditional statements and arrays
Manav
John
Jhinga_Man
Cs50 is not enough
Arnold
implement all the common data structures yourself in plain C and you will feel much better
Anonymous
Yeah I have already done that
You can do this course:
https://youtube.com/playlist?list=PLeURq1l0XRkj7EQMLeQwtaEMOXlP_DpEz&feature=shared
to learn Data Structures and Algorithms. You need to be familiar with some programming language before you do that so that you can try implementing what is taught in that course
Jhinga_Man
Write a C program to find if a graph is Eulerian (i.e., does it have an Eulerian cycle). Your input is
what is called as an Adjacency matrix A. If a graph has N nodes then A is an array of size N × N, with
A[i][j] = 1 if node i is connected to j and 0 otherwise. Your program should be able to read an Adjacency
matrix of any size N. For simplicity, let the graphs that are read be connected graphs, i.e., every node can
be reached from every other node
Like what I have study to understand this kind of question
Anonymous
Jhinga_Man
Okay got it thanks .... Helped a lot
Anonymous
Note to all: I have added the above YouTube Playlist to the videos section in the Resources channel. In future, if someone asks about references for DSA courses, kindly point them to the Resources channel in the pinned post.
Arnold
try implementing a depth first search or a breadth first search algorithm. That's a good and easy intro to any graph work.
Jhinga_Man
Arnold
Just google what depth first search is. You should definitely be able to do that before you do your assignment.
Wassim
Hasna
what is the difference between
string& name;
string name;
Michael
Light
Light
Hasna
Michael
Now both lead to compilation errors 😁
imminent
did you include <string>?
imminent
and prefix the string with a std::
VD
string& name;
string &name;
?
string alpha = "alpha";
string& ref = alpha;
string &ref1 = alpha;
cout << ref; //prints alpha
ref1.push_back('b');
cout << ref; //prints alphab
ref.push_back('e');
cout << ref1; //prints alphabe
alpha.push_back('t');
cout << ref; //prints alphabet
cout << ref1;//prints alphabet
Light
Writing string & name is the biggest crime one can comit 😂