Programs on OOPs

  1. Understand the below program and print the details of the student as shown in the sample test cases.

    • Student class is defined with public variables name, age, group and report.
    • Define setDetails() method that sets the student name, age, group and report values.
    • Define getDetails() method that prints all the details of the student.
    • Create an instance of class Student.
    • Set the name, age, group and report values of the student by taking the input from the student and call the setDetails() values
    • Now print the details of Student by calling the method getDetails()
    Sample Input:
    Govardhan
    37
    IT
    Pass
    Sample Output:
    Govardhan 37 IT Pass

    Click Here for Solution

  2. Let's write a simple program:

    • A base class Person and a derived class Student with Person as its base class.
    • Add two methods in the base class: setname()-takes the parameter name which sets the name value, and getname() which prints the name.
    • Add two methods in the derived class: setage()-takes the parameter age which sets the age value, and getage() which prints the age.
    • Create an instance of Student and name it as s1.
    • Take name and age as inputs from the console.
    • Call the setname() and setage() on this instance by passing the name and age parameters.
    • Call the getname() and getage() on this class, which prints the passed parameters
    Sample Input:
    Govardhan
    37
    Sample Output:
    Govardhan
    37

    Click Here for Solution

  3. Write a program that uses data encapsulation / data hiding

    • Define a class Car with two private variables engineno and modelno with values "EK1" and "VX6".
    • Define a public method setcarinfo(engineno, modelno).
    • Inside the method set each private variables with the values passed as arguments.
    • Define a public method getcarinfo(self) which prints all the information.
    • Create an instance of Car and name it hondacity.
    • Take enginenumber, modelnumber, color and year as an input from console.
    • Call the method setcarinfo() with inputs as parameters.
    • Now set the color and year by setting hondacity.color and hondacity.year.
    Sample Input:
    EK67G9
    SX6
    Blue
    2018
    Sample Output:
    EK67G9 SX6 Blue 2018

    Click Here for Solution