Need help in C++ please and thank you Need to help with questions 1, 2, 3 Will p
ID: 3722852 • Letter: N
Question
Need help in C++ please and thank you Need to help with questions 1, 2, 3 Will provide code below / array5.cpp - friend function // This program will compile, but it won't link without the set() definition #include <iostream> using namespace std; #define SIZE 10 class intArr ay { private: int a[SIZE]; public: void setVal(int index, int value); friend void print(intArray); }; void print(intArray x) { int i; for(i = 0; i < SIZE; i++) cout << "a[" << i << "] = " << x.a[i] << endl; } int main() { intArray num; int i; for(i = 0; i < SIZE; i++) num.setVal(i,i); print(num); return 0; } For question 3 // send.cpp void send(char * msg = "test", int times = 1); void send(char *, int); int main() { send(); send("hello"); / array5.cpp - friend function // This program will compile, but it won't link without the set() definition #include <iostream> using namespace std; #define SIZE 10 class intArr ay { private: int a[SIZE]; public: void setVal(int index, int value); friend void print(intArray); }; void print(intArray x) { int i; for(i = 0; i < SIZE; i++) cout << "a[" << i << "] = " << x.a[i] << endl; } int main() { intArray num; int i; for(i = 0; i < SIZE; i++) num.setVal(i,i); print(num); return 0; } For question 3 // send.cpp void send(char * msg = "test", int times = 1); void send(char *, int); int main() { send(); send("hello");ction Overloading & Default Arguments Homework we ve n create a single function name for a concept, and implement that name in seen how C++ lets us C++ lets us create functions that can be called in different ways t ways. This means we have less to teach the end user, and the malty s more readable. C++ supports this feature in two ways: code is overloading allows us to create more than one function with the same Function as all the functions have distinct argument lists. Function oading also prevents name clashes when using multiple libraries. Here are some overloaded examples of functions: void foo (int); void foolint, char) void foo (double): ault arguments are used with a single function when you want some of the Deraments to be automatically inserted by the compiler instead of writing them al ard yourself, every time you call the function. Here's a tunction with default int foo(float f, float f2 1.1, charx) You can declare a function more than once, but you may only give default arquments once. Only trailing arguments may be given default values, and once you start giving default values, all the rest of the arguments in the list must have defaults. 1. Name encoding schemes vary from compiler to compiler, and they are up to the implementer of the compiler. In addition, encoded names are not intended to be used directly by the programmer (which would mean, for instance, that an encoding scheme might legitimately be changed by a compiler vendor, which would require recompilation of all code but no other changes.) See if you can find out what encoded names will be generated by the compiler for the file array5.cpp Page 177 Focus on Object-Oriented Programming with C++
Explanation / Answer
1. function name will be _Z_functionname_argument
so, if we compile it for assembler stage -
.file "ecoded.cpp"
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.section .rodata
.LC0:
.string "a["
.LC1:
.string "] = "
.text
.globl _Z5print8intArray
.type _Z5print8intArray, @function
_Z5print8intArray:
.LFB1021:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
pushq %rbx
subq $24, %rsp
.cfi_offset 3, -24
movl $0, -20(%rbp)
.L3:
cmpl $9, -20(%rbp)
jg .L4
movl -20(%rbp), %eax
cltq
movl 16(%rbp,%rax,4), %ebx
movl $.LC0, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movq %rax, %rdx
movl -20(%rbp), %eax
movl %eax, %esi
movq %rdx, %rdi
call _ZNSolsEi
movl $.LC1, %esi
movq %rax, %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl %ebx, %esi
movq %rax, %rdi
call _ZNSolsEi
movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi
movq %rax, %rdi
call _ZNSolsEPFRSoS_E
addl $1, -20(%rbp)
jmp .L3
.L4:
nop
addq $24, %rsp
popq %rbx
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1021:
.size _Z5print8intArray, .-_Z5print8intArray
.globl main
.type main, @function
main:
.LFB1022:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $64, %rsp
movq %fs:40, %rax
movq %rax, -8(%rbp)
xorl %eax, %eax
movl $0, -52(%rbp)
.L7:
cmpl $9, -52(%rbp)
jg .L6
movl -52(%rbp), %edx
movl -52(%rbp), %ecx
leaq -48(%rbp), %rax
movl %ecx, %esi
movq %rax, %rdi
call _ZN8intArray6setValEii
addl $1, -52(%rbp)
jmp .L7
.L6:
subq $8, %rsp
pushq -16(%rbp)
pushq -24(%rbp)
pushq -32(%rbp)
pushq -40(%rbp)
pushq -48(%rbp)
call _Z5print8intArray
addq $48, %rsp
movl $0, %eax
movq -8(%rbp), %rsi
xorq %fs:40, %rsi
je .L9
call __stack_chk_fail
.L9:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1022:
.size main, .-main
.type _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB1031:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
cmpl $1, -4(%rbp)
jne .L12
cmpl $65535, -8(%rbp)
jne .L12
movl $_ZStL8__ioinit, %edi
call _ZNSt8ios_base4InitC1Ev
movl $__dso_handle, %edx
movl $_ZStL8__ioinit, %esi
movl $_ZNSt8ios_base4InitD1Ev, %edi
call __cxa_atexit
.L12:
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1031:
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
.type _GLOBAL__sub_I__Z5print8intArray, @function
_GLOBAL__sub_I__Z5print8intArray:
.LFB1032:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $65535, %esi
movl $1, %edi
call _Z41__static_initialization_and_destruction_0ii
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1032:
.size _GLOBAL__sub_I__Z5print8intArray, .-_GLOBAL__sub_I__Z5print8intArray
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I__Z5print8intArray
.hidden __dso_handle
.ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"
.section .note.GNU-stack,"",@progbits
So here
void print(intArray x) will be : _Z5print8intArray
2. default argument initialize in parameter only in declaration part.
// This program will compile, but it won't link without the set() definition
#include <iostream>
using namespace std;
#define SIZE 10
class intArray {
private:
int a[SIZE];
int x,y,z;
public:
void setVal(int index, int value);
void initialize(int a=1 , int b = 2, int c = 3);
friend void print(intArray);
void printPar(void);
};
void print(intArray x) {
int i;
for(i = 0; i < SIZE; i++)
cout << "a[" << i << "] = " << x.a[i] << endl;
}
void intArray:: printPar() {
cout << "x - " << x <<" y - " << y << " z - " << z << endl;
}
void intArray::initialize(int a , int b, int c)
{
x = a;
y = b;
z = c;
}
int main() {
intArray obj;
obj.initialize();
obj.printPar();
// intArray num;
// int i;
// for(i = 0; i < SIZE; i++)
// num.setVal(i,i);
// print(num);
return 0;
}
3.
send() - this function will call to first function because of all argmunents are default.
send("hello"); - this function also call to first one because of default arguments.
send("hello",3); - this will make complexity between first and second functin and will give error.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.