Pages

Wednesday, July 24, 2013

Polymorphism Public Single Inheritance code C++

/*
QUESTION:
                                           (Polymorphism)
 
 
Write a program to prepare the result of students by using Public -- Single Inheritance. The base



class is "std_info" and the derived class is "std_result". Major Attributes of both classes are as under:

(Add the other functionality).


std_info

private:
int reg_no; // To store registration # of student

char name[15]; // To store name of student

char address[50]; // To store address of

student


public:
void input_info(void); //Function to input

information

void print_info (void); //Function to print

information
///////////////////////////////////////////////////////////////////////
class std_result

private:
float sub1; //Marks of subject 1

float sub2; //Marks of subject 2

float sub3; //Marks of Subject 3

float average; //Average Marks

(sub1+sub2+sub3)/3

float total; //Total Marks (sub1+sub2+sub3)


public:
void input_info(); // Input marks of students

void print_info(); //Printing result card
 
Ø  In main, create an object "student" of "std_result".

Ø  Now create pointer "ptr" to std_info to manipulate all the student information (Note: You are



authorised to use only the pointer to std_info) i.e.

a. First get basic student information.

b. Next get details marks of student as input.

c. Finally print all the student information and his result card.
 




CODE:
*/

#include<iostream>
using namespace std;

class std_info{
int reg_no;
char name[15];
char address[50];

public:
 virtual void input_info(void){
  cout<<"\nEnter Registeration no: ";cin>>reg_no;

  cout<<"\nEnter name: ";
   cin.ignore();
  cin.getline(name,15,'\n');


  cout<<"\nEnter Address: ";
  cin.ignore();
  cin.getline(address,50,'\n');


 }
  virtual  void print_info(void){
  cout<<"\n REgisteration no is: "<<reg_no;
  cout<<"\nName is: "<<name;
  cout<<"\nAddress is : "<<address<<endl;
 }

};
class std_result: public std_info{
float sub1;
float sub2;
float sub3;
float average;
float total;

public:
 void input_info(){
cout<<"\nEnter marks of subject 1: ";cin>>sub1;
cout<<"\nEnter marks of subject 2: ";cin>>sub2;
cout<<"\nEnter marks of subject 3: ";cin>>sub3;
 }
 void print_info(){

  average=(sub1+sub2+sub3)/3;
  total = sub1+sub2+sub3;
cout<<"\nAverage marks are: "<<average;
cout<<"\nTotal marks are: "<<total<<endl;
 }

};


int main(){
std_result student;

std_info *ptr;
std_info *ptr1 = new std_info;
ptr1->input_info();

ptr = &student;
ptr->input_info();
ptr1->print_info();

ptr->print_info();

return 0;
}

No comments:

Post a Comment