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

Write A Program Using Static Member Function

The document contains code examples demonstrating static member functions, arrays of objects, and objects as function arguments in C++. The first example shows how to use a static member variable to count the number of objects created. The second example defines an array of employee objects and gets/sets data for each. The third example defines a time class and method to sum two time objects by passing them as arguments.

Uploaded by

Sumit Bedi
Copyright
© Attribution Non-Commercial (BY-NC)
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)
23 views7 pages

Write A Program Using Static Member Function

The document contains code examples demonstrating static member functions, arrays of objects, and objects as function arguments in C++. The first example shows how to use a static member variable to count the number of objects created. The second example defines an array of employee objects and gets/sets data for each. The third example defines a time class and method to sum two time objects by passing them as arguments.

Uploaded by

Sumit Bedi
Copyright
© Attribution Non-Commercial (BY-NC)
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

9. Write a program using static member function.

#include<iostream.h> class test { int code; static int count; public: void setcode(void) { code=++count; } void showcode(void) { cout<<"Object number: "<<code<<"\n"; } static void showcount(void) { cout<<"Count: "<<count<<"\n"; } };

int test::count; int main() { test t1,t2; t1.setcode(); t2.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); return 0; } OUTPUT: Count: 2 Count: 3 Object number: 1 Object number: 2 Object number: 3

10. Write a program by forming array of objects


#include<iostream.h> class employee { char name[30]; float age; public: void getdata(void); void putdata(void); }; void employee::getdata(void) { cout<<"Enter name: "; cin>>name; cout<<"Enter age: "; cin>>age; } void employee::putdata(void) { cout<<"Name: "<<name<<"\n";

cout<<"Age: "<<age<<"\n"; } const int size=3; int main() { employee manager[size]; for(int i=0;i<size;i++) { cout<<"\n Details of manager"<<i+1<<"\n"; manager[i].getdata(); } cout<<"\n"; for(i=0;i<size;i++) { cout<<"\n Manager"<<i+1<<"\n"; manager[i].putdata(); } return 0; }

OUTPUT: Details of manager1 Enter name: sumit Enter age: 24 Details of manager2 Enter name: zeenat Enter age: 19 Details of manager3 Enter name: hermione Enter age: 17

Manager1 Name: amrit Age: 24 Manager2 Name: zeenat Age: 19 Manager3 Name: hermione Age: 17

11. Write a program using objects as function arguments.


#include<iostream.h> class time { int hours; int minutes; public: void gettime(int h,int m) { hours=h; minutes=m; } void puttime(void) { cout<<hours<<"hours and"; cout<<minutes<<"minutes"<<"\n"; } void sum(time,time); }; void time::sum(time t1,time t2) { minutes=t1.minutes + t2.minutes; hours=minutes/60;

minutes=minutes%60; hours=hours+ t1.hours+ t2.hours; } int main() { time T1,T2,T3; T1.gettime(2,45); T2.gettime(3,30); T3.sum(T1,T2); cout<<"T1=";T1.puttime(); cout<<"T2=";T2.puttime(); cout<<"T3=";T3.puttime(); return 0; } OUTPUT: T1= 2 hours and 45 minutes T2= 3 hours and 30 minutes T3= 6 hours and 15 minutes

You might also like