1) Assume the variables ptr1 and ptr2 have been declared as pointers to integers
ID: 3559518 • Letter: 1
Question
1) Assume the variables ptr1 and ptr2 have been declared as pointers to integers. An array of 100 elements has been allocated, its pointer assigned to ptr1 , and its elements initialized to some values.
Write code to allocate an array of 200 elements, assign its pointer to ptr2, copy the 100 elements from ptr1 to the first 100 elements of ptr2, and initialize the remainder of the elements of ptr2 to 0.
2) Assume the existence of a class Player with the class specification shown below. Declare a derived class Quaterback that contains a single additional data member passCompletions (of type int) to hold the number of career pass completion of a football quaterback player. The derived class should also have a parametrized constructor to initialize the data members of an instance of Quaterback, accessor(s) and mutator(s), and a function named write to display the name, team, and passCompletions of an instance of Quaterback on the standard output devide.
For this question write an implementation of function write() of the derived class Quarterback.
class Player {
private:
string name;
string team;
public:
Player(string name=
Explanation / Answer
Ans 1)
/*Program for 1st question*/
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main(){
/*An array of 100 elements has been allocated*/
int *ptr1 = (int *)malloc(100 * sizeof(int));
/*Array elements initialized to their (index value + 1)*/
for(int i = 0; i < 100; i++){
ptr1[i] = i+1;
}
/*Allocate an array of 200 elements using calloc so that all elements will be initialized with 0*/
int *ptr2 = (int *)calloc(200,sizeof(int));
/*copy the 100 elements from ptr1 to the first 100 elements of ptr2*/
for(int i = 0; i < 100; i++){
ptr2[i] = ptr1[i];
}
/* print the values in array*/
for(int i = 0; i < 200; i++){
cout << ptr2[i] << " ";
}
return 0;
}
/*Output*/
[root@pankaj68(12:53:27) : chegg]# g++ allocation.cpp
[root@pankaj68(12:53:31) : chegg]# ./a.out
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Ans 2)
/*Program 2*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class Player {
private:
string name;
string team;
public:
Player(string n= " ", string t= " "){
name = n;
team = t;
}
void setName(string s){
name = s;
} // sets name
void setTeam(string t){
team = t;
} // sets team
string getName(){
return name;
} // returns name
string getTeam(){
return team;
} // returns team
void write(){
cout << "name:" << name <<endl;
cout << "team:" << team <<endl;
} // displays name and team on standard output device
};
class Quaterback : public Player {
private:
int passCompletions;
public:
/*Parametrise constructor*/
/* It will initialize all the data members of Player class and Quaterback Class */
Quaterback(string n = "", string t= "",int pC = 0) : Player(n,t),passCompletions(pC){
}
void write();
};
void Quaterback :: write(){
Player::write();
/*Using this function call will call the write function of Player class*/
cout << "passCompletions:"<<passCompletions;
}
int main(){
Quaterback Qb("pankaj","India",12);
Qb.write();
return 0;
}
/*Output*/
[root@pankaj68(12:55:41) : chegg]# g++ player.cpp
[root@pankaj68(12:55:46) : chegg]# ./a.out
name:pankaj
team:India
passCompletions:12
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.