DATA STRUCTURES THROUGH C++ LAB R19 Exercise -2

 

DATA STRUCTURES THROUGH C++ LAB  R19

Exercise -2 (Access) 

Write a program for illustrating Access Specifiers public, private, protected 

a) Write a program implementing Friend Function 

b) Write a program to illustrate this pointer 

c) Write a Program to illustrate pointer to a class



Exercise -2a:-   Write a program implementing Friend Function 

#include <iostream>

using namespace std;

class A

{

protected:

int x;

public:

A(int p)

{

x = p;

}

};

class B : public A

{

private:

int y;

public:

B(int p, int q) : A(p)

{

y = q;

}

void show()

{

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

cout<<"y = "<<y<<endl;

}

};

int main()

{

B obj(10, 20);

//Since show is public in class B, it is accessible in main function

obj.show(); //x is protected in A so it is accessible in B's show function

//y is not accessible in main as it is private to class B

//cout<<obj.y 

return 0;

}

Output for the above program is as follows:

x = 10

y = 20


Exercise -2b:-  Write a program to illustrate this pointer 

#include <iostream>

using namespace std;

class A

{

private:

int x;

int y;

public:

A(int x, int y)

{

this->x = x;

this->y = y;

}

void display()

{

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

cout<<"y = "<<y<<endl;

}

A& clone()

{

return *this;

}

};

int main()

{

A obj1(10, 20);

obj1.display();

A obj2 = obj1.clone();

obj2.display();

return 0;

}

Output for the above program is as follows:

x = 10

y = 20

x = 10

y = 20

Exercise -2c:-Write a Program to illustrate pointer to a class

#include <iostream>

using namespace std;

class A

{

private:

int x;

int y;

public:

A(int x, int y)

{

this->x = x;

this->y = y;

}

void display()

{

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

cout<<"y = "<<y<<endl;

}

};

int main()

{

A *ptr = new A(10, 30); //Here ptr is pointer to class A

ptr->display();

return 0;

}

Output for the above program is as follows:

x = 10

y = 30



No comments

File Uploading in PHP

  File Uploading in PHP PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variab...