Object Oriented Programming Practical 10

Question 1
WAP to declare a class student having datamembers as name and percentage .Write constructor to initialise these data members. Accept and display data for one object.
student.cpp
C
				#include<iostream.h>
#include<conio.h>
#include<string.h>


class Student 
{
	private:
		char name[50];
		float percentage;

	public:
		Student(char n[50], float p) 
		{
			strcpy(name,n);
			percentage=p;
		}

		void display() 
		{
			cout << "Name: " << name << endl;
			cout << "Percentage: " << percentage << endl;
		}
};

int main() 
{
    string sname;
    float spercent;

    cout << "Enter student's name: ";
    getline(cin, sname);
    cout << "Enter student's percentage: ";
    cin >> spercent;

    Student s(sname, spercent);

    cout << "\nStudent Details:\n";
    s.display();
	getch();
    return 0;
}

			
Output
Question 2
WAP to declare class time having data members as hrs, min,sec. Write a constructor to accept data and display for two objects.
time.cpp
C
				#include<iostream.h>
#include<conio.h>

class time
{
	int hour,mins,sec;
	
	public:
	time(int h,int m,int s)
	{
		hour=h;
		mins=m;
		sec=s;
	}

   void display()
	{
		cout<<hour<<":"<<mins<<":"<<sec;
	}
};

int main()
{
	clrscr();
	int hh,mm,ss;
	cout<<"\nEnter time 1:"<<endl;
	cout<<"Enter hour: ";
	cin>>hh;
	cout<<"Enter minutes: ";
	cin>>mm;
	cout<<"Enter seconds: ";
	cin>>ss;
	time t1(hh,mm,ss);
	cout<<"\nEnter time 2:"<<endl;
	cout<<"Enter hour: ";
	cin>>hh;
	cout<<"Enter minutes: ";
	cin>>mm;
	cout<<"Enter seconds: ";
	cin>>ss;
	time t2(hh,mm,ss);
	cout<<"\nTime 1 is: ";
	t1.display();
	cout<<"\nTime 2 is: ";
	t2.display();
	getch();
	return 0;
}

			
Output
Note: Change array size and for loop for 10 accounts.

Leave a Comment

Your email address will not be published. Required fields are marked *