Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views7 pages

Oops 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

EXERCISE 5

Write a C++ program with a class name getset. Keep a and b as private data members and
write the following functions in public mode.

i. void getdata() - Reads a and b values from the user and display the values

ii. void setdata(int,int) - Modify the values of private fields by assigning different set of numbers to
the private fields

iii. void display() – Display the values of two numbers a and b.

Note: Place the functions getdata and setdata outside the class and display inside the class

#include<iostream>

Using namespace std;

class getset{

int a,b;

public:

void getdata();

void setdata(int c,int d);

void display(){

cout<<"the value of a is "<<a<<endl;

cout<<"the value of b is "<<b<<endl;

};

void getset::getdata(){

cout<<"enter 2 values";

cin>>a>>b;

display();

void getset::setdata(int c,int d){


a=c;

b=d;

int main(){

getset obj;

obj.getdata();

obj.setdata(10,12);

obj.display();

b. Write a C++ program with a class name student. Keep roll number and name as private
data members and write the following functions in public mode.

i. void setdata(int srn, string sname) - Assigns values directly from the main function

ii. void putdata() - Displays the values to the user

#include<iostream>

using namespace std;

class student{

int roll;

string name;

public:

void setdata(int srn,string sname){

roll=srn;

name=sname;

void putdata(){
cout<<"The roll number of the student is "<<roll<<endl;

cout<<"The name of the student is "<<name<<endl;

};

int main(){

int r;

string s;

cout<<"enter roll number"<<endl;

cout<<"enter name"<<endl;

cin>>r>>s;

student obj;

obj.setdata(r,s);

obj.putdata();

c. Create a class named line and declare a constructor and destructor. The definitions are to
kept outside with a simple message in it. Keep the member functions as follows.

i. void setLength(double len ) - Assigns length of a line

ii. double getLength( void )- Returns the length of a line

Note: Place all member functions, constructor and destructor definitions outside the class.

#include<iostream>

using namespace std;

class line{

double l;
public:

line();

void setlength(double len);

double getlength();

~line();

};

line::line(){

cout<<"inside constructor"<<endl;

void line::setlength(double len){

l=len;

double line::getlength(){

return l;

line::~line(){

cout<<"inside destructor"<<endl;

int main(){

double length;

cout<<"enter length"<<endl;

cin>>length;

line obj;

obj.setlength(length);

cout<<"the length of entered text is "<<obj.getlength()<<endl;

d. Perform all operations like dot and cross product in a Vector class.
V = Xi + Yj + Zk Where, X, Y and Z are the magnitude of the vector V in the directions i , j and k
respectively.

#include<iostream>

using namespace std;

class vector{

int a1,b1,c1,a2,b2,c2;

public:

vector(){

};

vector(int a,int b,int c,int d,int e,int f){

a1=a;

b1=b;

c1=c;

a2=d;

b2=e;

c2=f;

void dot(){

int dot;

dot=a1*a2+b1*b2+c1*c2;

cout<<"The dot product of the 2 vectors is "<<dot<<endl;

void cross(){

cout<<"cross product of two vectors is ";

int i=b1*c2-c1*b2;

int j=a1*c2-c1*a2;

int k=a1*b2-a2*b1;
cout<<i<<"i + "<<j<<"j + "<<k<<"k "<<endl;

};

int main(){

int u,v,w,x,y,z;

cout<<"first vector is a1i +b1j +c1k"<<endl;

cout<<"enter a1,b1,c1"<<endl;

cin>>u>>v>>w;

cout<<"second vector is a2i +b2j +c2k"<<endl;

cout<<"enter a2,b2,c2"<<endl;

cin>>x>>y>>z;

vector obj(u,v,w,x,y,z);

obj.dot();

obj.cross();

5. Write a class named as bank. Include the member variables as depositor name, account
number, balance amount. Write appropriate member functions to assign initial values, deposit
an amount and display name & balance amount. Transfer amount from one account to another
and show the balance amount available in both accounts.

include<iostream>

using namespace std;

class bank{

string name;

int acct;

int amt;

public:

bank(string n,int acctno,int balamt){

name=n;
acct=acctno;

amt=balamt;

void deposit(int dep){

amt+=dep;

cout<<"amount deposited is "<<dep<<endl;

void display(){

cout<<"the name of account holder is "<<name<<endl;

cout<<"balance amount is now "<<amt<<endl;

void transfer(int trans,bank ob){

ob.amt+=trans;

cout<<ob.amt<<" transferred to account"<<ob.acct<<endl;

};

int main(){

bank obj("soniya",101,300);

obj.deposit(30);

obj.display();

bank obj1("Rahul",102,400);

obj.deposit(10);

You might also like