You are on page 1of 9

1) class Number

{
int num;
}
In the main function,
Number n1(8),n2(4),n3(20),n4(6),n5;
n5=n1-n2*n3/n4;
#include<iostream>
#include<conio.h>
using namespace std;
class Number
{
private:
int num;
public:
Number()
{
num=0;
}
Number(int num)
{
this->num=num;
}
Number operator*(Number &a)
{
return Number(this->num*a.num);
}
Number operator-(Number &a)
{
return Number(this->num-a.num);
}
Number operator/(Number &a)
{
return Number(this->num/a.num);
}
int getNum()
{
return num;
}
};
int main()
{
Number n1(8),n2(4),n3(20),n4(6),n5;
n5=n1-n2*n3/n4;
cout<<n5.getNum();
getch();
}
2) class sample
{
// here
};

void main()
{
sample s1;
sample s2(30,60);
sample s3("abc","xyz");
sample::myfun();
s1.fun1();
}
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class sample
{
private:
int n1,n2;
char *ch1,*ch2;
public:
sample()
{
cout<<"No - args"<<endl;
}
sample(int x,int y)
{
n1=x;
n2=y;
cout<<"Param int"<<endl;
}
sample(char *x,char *y)
{
ch1=new char[strlen(x)+1];
strcpy(ch1,x);
ch2=new char[strlen(y)+1];
strcpy(ch2,y);
cout<<"Param char"<<endl;
}
static void myfun()
{
cout<<"In MyFUN()"<<endl;
}
void fun1()
{
cout<<"In fun1()"<<endl;
}
};
void main()
{
sample s1;
sample s2(30,60);
sample s3("abc","xyz");
sample::myfun();
s1.fun1();
getch();
}
3) class sample

{
private:
char ch;
public:
sample(char ch)
{
this->ch=ch;
}
};
given above class.
write a global function "display" which will access "ch" directly and display it
.
call this function from main function.
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class sample
{
private:
char ch;
public:
sample(char ch)
{
this->ch=ch;
}
friend void display(sample &);
};
void display(sample &s)
{
cout<<s.ch;
}
int main()
{
sample s('a');
display(s);
getch();
}
4) class sample3
{
private:
int num;
public:
sample3()
{
}
sample3(int k)
{
}
sample3(char *ptr,float f)
{
}
sample3(int a,int b)

{
}
};
given above class, create 6 instances according to following requirement.
a) create
b) create
c) create
ructor.
d) create
e) create
f) create
tructor

an instance on heap which will call "no-arg" constructor.


an instance on stack which will call "two int argument" constructor
an instance on heap which will call "string and float" arguments const
an instance on heap which will call "two int argument" constructor
an instance on stack which will pass only one int.
an instance on stack which will call "string and float" arguments cons

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class sample3
{
private:
int num;
public:
sample3()
{
num=0;
cout<<"NO args"<<endl;
}
sample3(int k)
{
cout<<"Param int "<<endl;
}
sample3(char *ptr,float f)
{
cout<<"Param char float"<<endl;
}
sample3(int a,int b)
{
cout<<"param int int"<<endl;
}
~sample3()
{
cout<<"In destructor "<<num<<endl;
}
};
int main()
{
sample3 *s1=new sample3;
sample3 s2(1,2);
sample3 *s3=new sample3("abc",4.2f);
sample3 *s4=new sample3(1,2);
sample3 s5(5);
sample3 s6("pakya",4.25f);
delete s1;
delete s4;
delete s6;
getch();

5) Given
class Module1
{
int duration;
public:
Module1(int k)
{
duration=k;
}
};
class Module2
{
int duration;
public:
Module2(int k)
{
duration=k;
}
};
write a global function "check" which will take 2 modules (i.e. Module1 and Modu
le2 ) and check whether Modules are same or not.
call this "check" function from main function.
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class Module2;
class Module1;
bool check(Module1 &,Module2 &);
class Module1
{
int duration;
public:
Module1(int k)
{
duration=k;
}
friend bool check(Module1 &,Module2 &);
};
class Module2
{
int duration;
public:
Module2(int k)
{
duration=k;
}
friend bool check(Module1 &,Module2 &);
};

bool check(Module1 &m1,Module2 &m2)


{
return (m1.duration>m2.duration);
}
void main()
{
Module1 m1(2);
Module2 m2(3);
if(check(m1,m2))
cout<<"m1";
else cout<<"m2";
getch();
}
6) Make necessary changes in "One" and "Two" according to the statements given
in main function.
class One
{
public:
// here
};
class Two
{
public:
//here
};
void main()
{
One o(100,200);
int k=One::myfun1();
Two t('a');
t.fun2(o);
One o1(40);
cout<<o+o1<<endl;
char ch=t;
cout<<ch<<endl;
}
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class One
{
private:
static int s;
int n1,n2;
public:
One(int x)
{
n1=x;
cout<<"One 1 param"<<endl;
}
One(int x,int y)
{
n1=x;n2=y;

cout<<"One 2 param"<<endl;
}
static int myfun1()
{
return s;
}
int operator+(One &o)
{
return (n1+o.n1);
}
int getNum()
{
return n1;
}
};
int One::s=1;
class Two
{
private:
char c;
public:
Two(char ch)
{
c=ch;
cout<<"Two 1 param";
}
void fun2(One &o)
{
cout <<"In Two (One o.getNum())="<<o.getNum()<<endl;
}
operator char()
{
return c;
}
};
void main()
{
One o(100,200);
int k=One::myfun1();
Two t('a');
t.fun2(o);
One o1(40);
cout<<o+o1<<endl;
char ch=t;
cout<<ch<<endl;
getch();
}
7) Define a class "myclass" with following constructor
myclass(int k=0)
{
num=k;
}
in main function,

myclass m1;
myclass m2(100);
observe what happens
/*
0
100
*/
8) define a class with overloaded constructors and overloaded member functions.
from main function, try to call these overloaded constructors and member
functions

9) Define a class , write two member functions


void disp1(){ //some code }
void disp2(){ // some code }
Try to call disp1

from

disp2.

10) Define a class with constructor and member function


create object, call member function
now create a reference to that object, and using that reference try to c
all member function
11) Define two classes
a) myclass1 with default and parameterized constructors
and member function, disp
b) myclass2 with default and parameterized constructors
and member function disp
disp of myclass2 must be having reference of myclass1 as an argument. In
side disp of myclass2 u will call disp of myclass1.
12) Define a class with following members
int num1;
float fl;
char ch;
create object of this class in main function and print its size. (sizeof
)
13) Now add following member in above class and recheck the size
double dd;
14) Define a class "Customer" with following attributes
char *ccode;
char *name;
char *add;
double balance;
create only parameterized constructor , which will accept all these deta
ils.

make sure nobody copies one customer to another customer.


following are the member functions
void disp() // do not print balance
void setAdd(char*) // in case customer's add is changed
double getBalance() // only for balance
in main function, create customer objects and call the above methods.
15) Define a class as "MyString"
members
char *str;
int len;
member functions
void disp()
int length()
in main functions u should be able to do following things
a) MyString m1("Sachin");
b) cout<<m1.length();
c) MyString m2=m1;
d) MyString m3("Rahul");
e) m2=m3;
f) cout<< m2[1];
g) m3[0]='K'
h) m3=m1+m2;

You might also like