You are on page 1of 8

:Classes and Objects:

Program no:01
#include <iostream>

#include <string>

using namespace std;

class myinfo

private:

int names;

int fathername;

public:

int marks=90;

char grade='A';

};

int main()

myinfo Ash;

Ash.marks;

Ash.grade;

cout<<"MY grade"<<Ash.grade<<endl;

cout<<"MY marks"<<Ash.marks<<endl;
return 0;

:Output:

Program no:02
#include<iostream>

using namespace std;

class Room

public:

double height;

double breath;
double length;

double calculatearea()

return length * breath;

double calculatevolume()

return length * breath * height;

};

int main()

Room room1;

room1.length = 98;

room1.breath = 67;

room1.height = 23;

cout<<"Area of a room"<<room1.calculatearea()<<endl;

cout<<"Area of a room"<<room1.calculatevolume()<<endl;

return 0;

:output:
PUBLIC,PRIVATE AND PROTECTED
#include <iostream>

using namespace std;

class base

private:

int x;

protected:

int y;

public:

int z;
base() //constructor to initialize data members

x = 1;

y = 2;

z = 3;

};

class derive: private base

//y and z becomes private members of class derive and x remains


private

public:

void showdata()

cout << "x is not accessible" << endl;

cout << "value of y is " << y << endl;

cout << "value of z is " << z << endl;

};
int main()

derive a; //object of derived class

a.showdata();

//a.x = 1; not valid : private member can't be accessed outside of


class

//a.y = 2; not valid : y is now private member of derived class

//a.z = 3; not valid : z is also now a private member of derived class

return 0;

} //end of program

Output

Program no:02
#include <iostream>
using namespace std;
class A
{
private:
int a, b;

public:
void getdata(int x, int y)
{
a = x;
b = y;
}
void showdata()
{
cout << "The sum of two private integer data members are =
" << a + b << endl;
cout << "The product of two private integer data members
are = " << a * b << endl;
}
};
int main()
{
A obj;
obj.getdata(4, 5);
obj.showdata();
return (0);
}

Output

You might also like