Q4. Define a class Counter having an attribute value . Provide a constructor tha
ID: 3541210 • Letter: Q
Question
Q4. Define a class Counter having an attribute value. Provide a constructor that initializes value to zero. Also provide following methods:
? Increment () : that increment the value by one.
? Decrement () : that decrement the value by one.
Q5. Define a function Reset that takes a Counter type object as input and resets its value to zero. Make this function a friend of Counter class
Q6. Define a class Student that has following attributes:
Name: allocated dynamically by a character pointer.
Rollno: an integer.
Marks: a double type array of 5 elements.
Percentage: a float
Include a constructor that takes values of Name, Rollno and Marks from user as input. Also include following methods:
CalculatePercentage: that adds all 5 elements of array Marks and calculate percentage according to formula Percentage = (Total marks/ 500 )*100 and stores result in member variable Percentage.
Grade: that calls CalculatePercentage method and displays the grade accordingly
Write a driver program to test your class.
Solve Lab#3 ::
Q1. Write a program that takes the record of 10 students from user in an array and display all the records.[use Student class, defined Q6]
Q2.Define a pointer to student class to access the contents of array defined in Q1. Allow user to search a record in array by means of Rollno
Q3. Develop a class to represent an integer array. The member variables include an integer to represent the size of array and an integer pointer to represent the address of the first element of the array. The user is allowed to create an array at runtime using this class. Include appropriate constructors (parameterized and copy). Also include a method that adds the contents of array.
Q4.Consider following code:
class myclass
{int data[2];
public:
int* p;
public:
myclass()
{p=data;}
};
int main()
{ myclass* cp;
cp=new myclass[3];
return 0;
}
How would you access the contents of data of each element of myclass array? Add code in the above program to do the following;
Explanation / Answer
Q4. Define a class Counter having an attribute value. Provide a constructor that initializes value to zero.
Also provide following methods:
? Increment () : that increment the value by one.
? Decrement () : that decrement the value by one.
class Counter
{
private:
int value;
public:
Counter()
{
value=0;
}
void Increment()
{
value++;
}
void Decrement()
{
value--;
}
friend void reset_counter();
};
Q5. Define a function Reset that takes a Counter type object as input and resets its value to zero.
Make this function a friend of Counter class
void reset_counter()
{
value = 0;
}
Q6. Define a class Student that has following attributes:
Name: allocated dynamically by a character pointer.
Rollno: an integer.
Marks: a double type array of 5 elements.
Percentage: a float
Include a constructor that takes values of Name, Rollno and Marks from user as input. Also include following methods:
CalculatePercentage: that adds all 5 elements of array Marks and calculate percentage according to formula Percentage =
(Total marks/ 500 )*100 and stores result in member variable Percentage.
Grade: that calls CalculatePercentage method and displays the grade accordingly
Write a driver program to test your class.
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class Student
{
private:
char *Name;
int Rollno;
double Marks[5];
float Percentage;
public:
Student(char* str,int no,double marks[5])
{
Name = new char[strlen(Name)+1];
strcpy(Name,str);
Rollno = no;
for(int i=0; i<5; i++)
Marks[i] = marks[i];
}
void set_data(char* str,int no,double marks[5])
{
Name = new char[strlen(Name)+1];
strcpy(Name,str);
Rollno = no;
for(int i=0; i<5; i++)
Marks[i] = marks[i];
}
void CalculatePercentage()
{
float sum = 0;
for(int i=0; i<5; i++)
sum = sum+Marks[i];
Percentage = (sum/500)*100;
}
void Grade()
{
CalculatePercentage();
cout << "student name is " << Name << " and his roll no is " << Rollno << endl;
if(Percentage > 90) cout << "Grage is A " << endl;
else if(Percentage > 80) cout << "Grage is B " << endl;
else if(Percentage > 70) cout << "Grage is C " << endl;
else if(Percentage > 60) cout << "Grage is D " << endl;
else cout << "Grage is E " << endl;
}
};
int main()
{
char *name = "Ashok Kumar";
int rollno = 123;
double marks[] = {91,92,93,94,95};
Student S1(name,rollno,marks);
S1.Grade();
return 0;
}
Solve Lab#3 ::
Q1. Write a program that takes the record of 10 students from user in an array and display all the records.[use Student class, defined Q6]
Student S_array[10];
char str[90];
int rollno;
double marks[5];
for(int i=0; i<10; i++)
{
cout << "Enter student name";
cin >> str;
cout << "Enter student Rollno";
cin >> rollno;
for(int j=0; j<5; j++)
{
cout << "Enter marks " << (j+1) << " : " << endl;
cin >> marks[j];
}
S_array[i] = new Student(str,rollno,marks);
}
for(int i=0; i<10; i++)
{
S_array[i].set_data(name,rollno,marks);
}
Q2.Define a pointer to student class to access the contents of array defined in Q1.
Allow user to search a record in array by means of Rollno
Student *pointer_to_array;
pointer_to_array = S_array'
Q3. Develop a class to represent an integer array. The member variables include an integer to represent the size of array and an integer
pointer to represent the address of the first element of the array. The user is allowed to create an array at runtime using this class.
Include appropriate constructors (parameterized and copy). Also include a method that adds the contents of array.
class integer_array
{
private:
int size;
int *array;
public:
integer_array(int k,int arr[])
{
size = k;
array = new int[size];
for(int i=0; i<k; i++)
array[i] = arr[i];
}
integer_array(const integer_array& other)
{
size = other.size;
for(int i=0; i<k; i++)
array[i] = other.array[i];
}
};
Q4.Consider following code:
class myclass
{
int data[2];
public:
int* p;
public:
myclass()
{
p=data;
}
};
int main()
{
myclass* cp;
cp=new myclass[3];
return 0;
}
How would you access the contents of data of each element of myclass array? Add code in the above program to do the following;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.