Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

PRACTICE PROBLEM 9

NAME – HARSHIT JOSHI


REG NO. – 22BCE3398

Q1) Write a cpp program for finding total & avg for single students. (Note:
create class & declare variable s1, s2, s3, s4, s5, total, average;

& create two member function

void getdata(int s1,int s2,int s3,int s4,int s5) &

void display( )

then should pass value from main as like

void getdata(75,85,96,75,88);

void display( ); //display function will print tot & avg.

SOURCE CODE:
#include<iostream>

using namespace std;

class Student{
int s1,s2,s3,s4,s5,tot;

float avg;

public:

void getdata(int m1,int m2,int m3,int m4,int m5){

s1=m1;

s2=m2;

s3=m3;

s4=m4;

s5=m5;

void display(){

tot=s1+s2+s3+s4+s5;

avg=tot/5.0;

cout<<"Total marks is: "<<tot<<endl;

cout<<"Average marks is: "<<avg<<endl;

};

int main(){

Student s;

s.getdata(36,37,96,95,18);
s.display();

return 0;

OUTPUT:
Q2) Develop a CPP program for static data member

(Note:

create a data member in public part static int a; &

assign the value zero to variable a

create data member

void func1();

a++;

then create three object c1,c2,c3 & call this func1 using that objects.

then print a value into main program.

SOURCE CODE:
#include <iostream>

using namespace std;

class MyClass {

public:

static int a;

void func1() {
a++;

};

int MyClass::a = 0;

int main() {

MyClass c1, c2, c3;

c1.func1();

c2.func1();

c3.func1();

cout << "Value of a: " << MyClass::a << endl;

return 0;

OUTPUT:
Q3) Develop a CPP program for static data member & member function

(Note:

create a data member in public part static int a; &

assign the value zero to variable a

create data function like

static void func1();

a++;

then call this func1 without using objects.

then print a value into main program.

SOURCE CODE:
#include <iostream>

using namespace std;

class MyClass {

public:

static int a;

static void func1() {


a++;

};

int MyClass::a = 0;

int main() {

MyClass::func1();

cout << "Value of a: " << MyClass::a << endl;

return 0;

OUTPUT:

You might also like