0% found this document useful (0 votes)
47 views7 pages

Oops 2

The document describes a HackerRank problem to create a Student class with getter and setter methods for student attributes like age, name, standard. It includes code for a Student class that implements these methods along with a to_string() method to output the student details as a comma separated string. The code takes input for a student's details and uses the Student class methods to output the details.

Uploaded by

satwik.bhutani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views7 pages

Oops 2

The document describes a HackerRank problem to create a Student class with getter and setter methods for student attributes like age, name, standard. It includes code for a Student class that implements these methods along with a to_string() method to output the student details as a comma separated string. The code takes input for a student's details and uses the Student class methods to output the details.

Uploaded by

satwik.bhutani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

HACKERRANK PROBLEM 1

AIM:
You have to create a class, named Student, representing the student's details, as mentioned above,
and store the data of a student. Create setter and getter functions for each element; that is, the class
should at least have following functions:

get_age, set_age

get_first_name, set_first_name

get_last_name, set_last_name

get_standard, set_standard

Also, you have to create another method to_string() which returns the string consisting of the above
elements, separated by a comma(,). You can refer to stringstream for this.

Input Format

Input will consist of four lines.


The first line will contain an integer, representing the age. The second line will contain a string,
consisting of lower-case Latin characters ('a'-'z'), representing the first_name of a student.
The third line will contain another string, consisting of lower-case Latin characters ('a'-'z'),
representing the last_name of a student.
The fourth line will contain an integer, representing the standard of student.

Note: The number of characters in first_name and last_name will not exceed 50.

SOURCE CODE:
#include <iostream>
#include <sstream>
using namespace std;

/*
Enter code for class Student here.
Read statement for specification.
*/
class Student{
int age, standard;
string first_name, last_name;
public:

void set_age(int age){

this->age=age;
}
void set_standard(int standard){
this->standard=standard;
}
void set_first_name(string first_name){
this->first_name=first_name;
}
void set_last_name(string last_name){
this->last_name=last_name;
}
int get_age(){
return age;
}
int get_standard(){
return standard;
}
string get_last_name(){
return last_name;
}
string get_first_name(){
return first_name;
}
void to_string(){

cout<<get_age()<<","<<get_first_name()<<","<<get_last_name()<<","<<get_standard();
}
};
int main() {
int age, standard;
string first_name, last_name;

cin >> age >> first_name >> last_name >> standard;

Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name);
st.set_last_name(last_name);

cout << st.get_age() << "\n";


cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
cout << st.get_standard() << "\n";
cout << "\n";
st.to_string();

return 0;
}
OUTPUT:
HACKERRANK PROBLEM 2
AIM:
A class defines a blueprint for an object. We use the same syntax to declare objects of a class as we
use to declare variables of other basic types. For example:

Box box1; // Declares variable box1 of type Box

Box box2; // Declare variable box2 of type Box

Kristen is a contender for valedictorian of her high school. She wants to know how many students (if
any) have scored higher than her in the exams given during this semester.

Create a class named with the following specifications:

An instance variable named to hold a student's exam scores.

A void input() function that reads integers and saves them to .

An int calculateTotalScore() function that returns the sum of the student's scores.

Input Format

Most of the input is handled for you by the locked code in the editor.

In the void Student::input() function, you must read scores from stdin and save them to
your instance variable.

SOURCE CODE:
class Student{
int scores [5];
public:
void input() {
for (int i=0;i < 5;i++) {
cin>> scores[1];
}
}
int calculateTotaLScore() {
int sum =0;
for (int i = 0; i < 5; +++) {
sum += scores[1];
}
return sum;
}
int main() {
int n; // number of students
cin >> n;
Student *s = new Student[n]; // an array of n students
for(int i = 0; i < n; i++){
s[i].input();
}

// calculate kristen's score


int kristen_score = s[0].calculateTotalScore();

// determine how many students scored higher than kristen


int count = 0;
for(int i = 1; i < n; i++){
int total = s[i].calculateTotalScore();
if(total > kristen_score){
count++;
}
}

// print result
cout << count;

return 0;
}

OUTPUT:
PROGRAM 8

AIM:
Write a program to run-time polymorphism.

SOURCE CODE:
#include <iostream>
using namespace std;
class X {
public:
void Print()
{
cout << "Base Function" << endl;
}
};
class Y : public X {
public:
void Print()
{
cout << "Derived Function" << endl;
X::Print();
}
};
int main()
{
Y Child;
Child.Print();
return 0;
}

OUTPUT:
PROGRAM 9
AIM:
Write a program to overload new/delete operator in a class.

SOURCE CODE:
#include <iostream>
using namespace std;
class student{
int math_score;
int sci_score;
public:
void *operator new(size_t s){
cout<<"called new operator"<<endl;
void *p=malloc(s);
return p;
}
void operator delete(void *p){
cout<<"called delete operator";
free(p);
}
};
int main(){
student *s=new student();
delete s;
}

OUTPUT:

You might also like