0% found this document useful (0 votes)
55 views22 pages

Thapar Object Oriented Programming Assignment Lab 4-5

The document contains code snippets demonstrating different concepts of OOPs in C++ like constructors, parameterized constructors, copy constructors, destructors, friend functions, friend classes, inheritance, polymorphism etc. Each code snippet is explaining a different concept through a small program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views22 pages

Thapar Object Oriented Programming Assignment Lab 4-5

The document contains code snippets demonstrating different concepts of OOPs in C++ like constructors, parameterized constructors, copy constructors, destructors, friend functions, friend classes, inheritance, polymorphism etc. Each code snippet is explaining a different concept through a small program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT-4…

1 (a) //default
#include <iostream>
using namespace std;
class construct
{
public:
int a, b;
construct()
{
a = 10;
b = 20;
}
};

int main()
{
construct c;
cout << "a: " << c.a << endl<< "b: " << c.b;
return 1;
}

(b) // parameterized

#include<iostream>

using namespace std;

class ABC

{
private:

int length,breadth,x;

public:

ABC (int a,int b)

length = a;

breadth = b;

int area( )

x = length * breadth;

return x;

void display( )

cout << "Area = " << x << endl;

};

int main()

ABC c(2,4);

c.area();

c.display();

ABC c1= ABC(4,4);

c1.area();

c1.display();

return 0;
}

(c) // copy

#include <iostream>

using namespace std;

class A

public:

int x;

A(int a)

x=a;

A(A &i)

x = i.x;

};

int main()

A a1(20);

A a2(a1);

cout<<a2.x;

return 0;

(d) //one argument


#include<iostream>

using namespace std;

class X {

int a;

public:

X(int j) { a = j; }

int geta() { return a; }

};

int main()

X ob = 99;

cout << ob.geta();

return 0;

2 (a) #include <iostream>

using namespace std;

class pros{

int data1;

int data2;

int data3;

public:

pros(int a, int b=8, int c=5){

data1 = a;

data2 = b;

data3 = c;

}
void printData();

};

void pros :: printData(){

cout<<"The value of data1, data2 and data3 is "<<data1<<", "<< data2<<" and "<< data3<<endl;

int main(){

pros k(15, 19);

k.printData();

return 0;

(b) // destructor

#include<iostream>

using namespace std;

class Demo {

private:

int num1, num2;

public:

Demo(int n1, int n2) {

cout<<"Inside Constructor"<<endl;

num1 = n1;

num2 = n2;

void display() {

cout<<"num1 = "<< num1 <<endl;

cout<<"num2 = "<< num2 <<endl;

}
~Demo() {

cout<<"Inside Destructor";

};

int main() {

Demo obj1(10, 20);

obj1.display();

return 0;

3 (b)

(a)
(c)
(d)

4 (a)

#include <iostream>
using namespace std;
class cl {
int i;
public:
cl(int j) { i=j; }
int get_i() { return i; }
};

int main()
{
cl ob(88), *p;
p = &ob;
cout << p->get_i();
return 0;
}

Output
88
(b) //this pointer

#include <iostream>

using namespace std;

class Demo {

private:

int num;

char ch;

public:

void setMyValues(int num, char ch){

this->num =num;

this->ch=ch;

void displayMyValues(){

cout<<num<<endl;

cout<<ch;

};

int main(){

Demo obj;

obj.setMyValues(100, 'A');

obj.displayMyValues();

return 0;

5 (a)

#include<iostream>

using namespace std;

int main()

{
int *p;

p = new int(100);

cout << "Value is: " << *p;

delete p;

return 0;

(b)

#include <iostream>

#include <new>

using namespace std;

int main()

{
int *p, i;

p= new int[3];
for(i=0; i<3; i++ )
p[i] = i;
for(i=0; i<3; i++)
cout << p[i] << " ";
delete [] p; // release the array
return 0;

#include <iostream>

using namespace std;

const int MAX = 3;

int main () {

int var[MAX] = {10, 90, 700};


int *ptr[MAX];

for (int i = 0; i < MAX; i++) {

ptr[i] = &var[i];

for (int i = 0; i < MAX; i++) {

cout << "Value of var[" << i << "] = ";

cout << *ptr[i] << endl;

return 0;

#include<iostream>

using namespace std;

class Test {

public: Test() {cout << "In constructor"<<endl; }

~Test() { cout << "In destructor"<<endl; }

};

void myfunc()

static Test obj;

int main() {

cout << "Start main()"<<endl;


myfunc();

cout << "End main()"<<endl;

return 0;

8 (a) //friend function

#include<iostream>

Using namespace std;

class Distance {

private:

int meter;

friend int addFive(Distance);

public:

Distance() : meter(0) {}

};

int addFive(Distance d) {

d.meter += 5;

return d.meter;

int main() {

Distance D;

cout << "Distance: " << addFive(D);

return 0;

}
(b) //friend class

#include<iostream>

using namespace std;

class A

int x =5;

friend class B;

};

class B

public:

void display(A &a)

cout<<"value of x is : "<<a.x;

};

int main()

A a;

B b;

b.display(a);

return 0;

ASSIGNMENT-5…
1
#include<iostream>
using namespace std;
class base
{
public:
int x;
void getx()
{
cout<<"enter value of x";
cin>>x;
}
};
class derived:public base
{
public:
int y;
int z;
void gety()
{
cout<<"enter y";
cin>>y;
}
void sum()
{
z=x+y;
cout<<"sum is:"<<z;
}
};
int main()
{
derived d;
d.getx();
d.gety();
d.sum();
return 0;
}

2
#include<iostream>
using namespace std;
class parent
{
protected:
int id;
};
class child:protected parent
{
public:
void setid(int id1)
{
id=id1;
}
void display(){
cout<<id;
}
};
int main()
{
child c;
c.setid(768);
c.display();
return 0;
}

3 (a)
#include<iostream>
using namespace std;
class parent
{
public:
int id;
};
class child:protected parent
{
public:
void setid(int id1)
{
id=id1;
}
void display()
{
cout<<id;
}
};
int main()
{
child c;
c.setid(768);
c.display();
return 0;
}

(b)
#include <iostream>
using namespace std;

class Base {
private:
int pvt = 1;

protected:
int prot = 5;

public:
int pub = 7;
int getPVT() {
return pvt;
}
};

class PrivateDerived : private Base {


public:
int getProt() {
return prot;
}
int getPub() {
return pub;
}
};

int main() {
PrivateDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}

4 (b) // friend class

include<iostream>

using namespace std;

class a

int x;

friend class b;

public:
a()

x=60;

};

class b

int y;

public:

b()

y=90;

int sum()

a a1;

return a1.x+y;

};

int main()

b b1;

cout<<b1.sum();

return 0;

}
(a) //friend function

#include<iostream>

using namespace std;

class a

int x;

public:

void setdata(int i)

x=i;

friend void max(a,z);

};

class z;

int y;

public:

void setdata(int i)

y=i;

friend void max(a,z);


};

void max(a a1,z z1)

if(a1.x>=z1.y)

cout<<a1.x;

else

cout<<z1.y;

int main()

a a1;

z z1;

a1.setdata(80);

z1.setdata(200);

max(a1,z1);

return 0;

KASHISH
102117150 (CS6)

You might also like