Professional Documents
Culture Documents
{
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
#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 &);
};
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
from
disp2.