Oops 2
Oops 2
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
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:
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;
Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name);
st.set_last_name(last_name);
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:
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.
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();
}
// 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: