#include <iostream> using namespace std; // Here are class and function definiti
ID: 3904644 • Letter: #
Question
#include <iostream>
using namespace std;
// Here are class and function definitions used in program:
// This program does run!!! Indicate output for each numbered line.
// If output is a memory reference instead of data, indicate "HEX"
// A class definition
class A
{
public:
A() { cout << "makeA" << endl; data = 42;}
void fun() {cout << data << endl;}
private:
int data;
};
// another class
class B : public A
{
public:
B() { cout << "makeB" << endl; data = 99;}
void fun() {cout << data << endl;}
private:
int data;
};
// a C-style struct
struct record
{
int SID;
char name[10];
double salary;
};
// simple hash function
int hash_code(record person)
{
return person.SID % 10;
}
// some functions using above classes
void f(A one) {cout << "f works" << endl;}
void g(B two) {cout << "g works" << endl;}
// The main driver: Indicate output for each numbered line.
void main()
{
A *a = new A(); // 1.
B *b = new B(); // 2.
a->fun(); // 3.
b->fun(); // 4.
a = b;
a->fun(); // 5.
b->fun(); // 6.
f(*a); // 7.
g(*b); // 8.
// Some arrays and pointer math:
int array[] = {3,6,9,12,15,18};
int *pa = array;
cout << array << endl; // 9.
cout << pa << endl; // 10.
cout << *(pa + 2) << endl; // 11.
cout << (*pa + 2) << endl; // 12.
cout << *pa++ << endl; // 13.
cout << *pa << endl; // 14.
// Put records into a hash table
record hash_table[10];
record init = {-1,"-1",-1}; // yes this does work
for (int i=0; i<10; i++) hash_table[i] = init;
record sally = {2279128,"sally", 23234.21};
record bobbie = {9483921,"bobbie", 56192.23};
record billy = {5649128,"billy", 85423.92};
hash_table[hash_code(sally)] = sally;
hash_table[hash_code(bobbie)] = bobbie;
hash_table[hash_code(billy)] = billy;
cout << sally.SID << endl; // 15.
cout << sally.name[1] << endl; // 16.
cout << sally.salary << endl; // 17.
cout << hash_code(sally) << endl; // 18.
for (int i=0; i<10; i++)
cout << hash_table[i].name << " "; // 19.
}
Explanation / Answer
PLEASE GIVE THUMBS UP, THANKS
DATA :
#include <iostream>
using namespace std;
// Here are class and function definitions used in program:
// This program does run!!! Indicate output for each numbered line.
// If output is a memory reference instead of data, indicate "HEX"
// A class definition
class A
{
public:
A() { cout << "makeA" << endl; data = 42;}
void fun() {cout << data << endl;}
private:
int data;
};
// another class
class B : public A
{
public:
B() { cout << "makeB" << endl; data = 99;}
void fun() {cout << data << endl;}
private:
int data;
};
// a C-style struct
struct record
{
int SID;
char name[10];
double salary;
};
// simple hash function
int hash_code(record person)
{
return person.SID % 10;
}
// some functions using above classes
void f(A one) {cout << "f works" << endl;}
void g(B two) {cout << "g works" << endl;}
// The main driver: Indicate output for each numbered line.
int main()
{
A *a = new A(); // 1. calling constructor A() therefore it will display "makeA",and data=42
B *b = new B(); // 2.calling constructor B() and B also extends class A so first A() constructor will be called
//and then B() constructtor so output : "makeA" and "makeB" A's data=42 and B's data=99
a->fun(); // 3. it will call fun() of class A so output : "42"
b->fun(); // 4.it will call fun() of class B so output : "99"
a = b;
a->fun(); // 5. Since address stored in b is passed to a, so a point to *b, so it will call a's fun() so output: "42"
b->fun(); // 6.4.it will call fun() of class B so output : "99"
f(*a); // 7.Display "f works"
g(*b); // 8.Display "g works"
// Some arrays and pointer math:
int array[] = {3,6,9,12,15,18};
int *pa = array;
cout << array << endl; // 9.HEX
cout << pa << endl; // 10.HEX
cout << *(pa + 2) << endl; // 11.it means base address + 2 more location i.e arra[2]=9, i.e output :"9"
cout <<(*pa + 2) << endl; // 12.*pa=3 and 3+2=5 so output : 5
cout <<*pa++ << endl; // 13.it means first display content of pa[0] output:"3", now p[0]=3+1=4
cout << *pa << endl; // 14. output : 4
// Put records into a hash table
record hash_table[10];
record init = {-1,"-1",-1}; // yes this does work
for (int i=0; i<10; i++) hash_table[i] = init;
record sally = {2279128,"sally", 23234.21};
record bobbie = {9483921,"bobbie", 56192.23};
record billy = {5649128,"billy", 85423.92};
hash_table[hash_code(sally)] = sally;
hash_table[hash_code(bobbie)] = bobbie;
hash_table[hash_code(billy)] = billy;
cout << sally.SID << endl; // 15. output : 2279128 becuase record sally = {2279128,"sally", 23234.21}; here first entry is SID
cout << sally.name[1] << endl; // 16. it will display 2nd charater from name array name[0]=s, name[1]=a,name[2]=l snd so on, name[1]=a is output
cout << sally.salary << endl; // 17.dispaying 3rd entry of sally therefor output 23234.21
cout << hash_code(sally) << endl; // 18. 2279128 mod 10 is 8, so output is 8
for (int i=0; i<10; i++)
cout << hash_table[i].name << " "; // 19. since stored in 9th location then bobbie is in 2nd location and billy again in 9th location because jash code of sally and billy is same so sally is overwritten bu billy
//so output: "-1 bobbie -1 -1 -1 -1 -1 -1 billy -1"
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.