Given the following: struct cell { int num1; float num2; } ; struct Person { int
ID: 3528668 • Letter: G
Question
Given the following: struct cell { int num1; float num2; } ; struct Person { int age; float income; }; Cell t, s; Person p1, p2, p3; s.num1 = 0; s.num2 = 2.5; //initialize s p2.age = 25; p2.income = 9999.99; //initialize p2 t = s; //assignment one p1 = s; //assignment two p2 = p3; //assignment three p3.age = s.num1; //assignment four p3.income = s.num1; //assignment five p2.age = t.num2; //assignment six 2) (12pts) For each assignment state whether it will compile or not and why. Also list if there will be any coercions or widening or shortening of datatypes. 3) (6pts) Analyze and write a comparison of CExplanation / Answer
struct cell {
int num1;
float num2;
} ;
struct Person {
int age;
float income;
};
struct cell t, s;
Person p1, p2, p3;
s.num1 = 0;
s.num2 = 2.5; //initialize s
p2.age = 25;
p2.income = 9999.99; //initialize p2
t = s; //assignment one good, same types
p1 = s; //assignment two error, different types with no overload of = sign
p2 = p3; //assignment three good, same types
p3.age = s.num1; //assignment four good, both are ints
p3.income = s.num1; //assignment five good, but widening of int to float
p2.age = t.num2; //assignment six good, but shortening num2 by casting from float to int
malloc/free works similar to new/delete. Both of them try to allocate memory.
On failure to allocate memory, malloc returns a null pointer and new would thow std::bad_alloc.
new/delete calls the constructor/destructor of object it creates while malloc/free does not.
new/delete is considered to be safer in the sense that Cpp has a type-safety check.
refer to this link for your opinion:
http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.