Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Oops

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 60

aAccess Specifiers

class MyClass {
public:

};

The public keyword is an access specifier. Access


specifiers define how the members (attributes and
methods) of a class can be accessed. In the example
above, the members are public - which means that they
can be accessed and modified from outside the code.

However, what if we want members to be private and


hidden from the outside world?

In C++, there are three access specifiers:

 public - members are accessible from outside the class


 private - members cannot be accessed (or viewed)
from outside the class
 protected - members cannot be accessed from outside
the class, however, they can be accessed in inherited
classes. You will learn more about Inheritance later.

In the following example, we demonstrate the differences


between public and private members:

Example
class MyClass {
public: int x;
private:
int y;
};

int main() {
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}

If you try to access a private member, an error occurs:


error: y is private

Note: By default, all members of a class are private if you


don't specify an access specifier:

Example
class MyClass {
int x; // Private attribute
int y; // Private attribute
};

/ C++ program to demonstrate private

// access modifier

Program – 1 :
#include<iostream>
using namespace std;
class Circle
{
private: double radius;
// public member function
public: double compute_area()
{
return 3.14*radius*radius;
}
};
int main()
{
Circle obj;
obj.radius = 1.5;
cout << "Area is:" << obj.compute_area();
return 0;
}
Output:

In function 'int main()':

11:16: error: 'double Circle::radius' is private

double radius;

31:9: error: within this context

obj.radius = 1.5;

^
The output of the above program is a compile time error because we are
not allowed to access the private data members of a class directly from
outside the class. Yet an access to obj.radius is attempted, but radius
being a private data member, we obtained the above compilation error.

However, we can access the private data members of a class indirectly


using the public member functions of the class.

Program – 2 :

// C++ program to demonstrate private access modifier

#include<iostream>

using namespace std;

class Circle

private: double radius;

public: void compute_area(double r)

radius = r;

double area = 3.14*radius*radius;

cout << "Radius is: " << radius << endl;

cout << "Area is: " << area;

}
};

int main()

Circle obj;

obj.compute_area(1.5);

Output:

Radius is: 1.5

Area is: 7.065


Program 3 and 4 lab work
Program 5
A program to assign values to the data members of a class such as
day, month, year and display the contents of the class on the screen.

#include <iostream>
using namespace std;
{
class date {
public :
int day, month, year;
};
int main()
{
date today;
today.day = 10;
today.month = 5;
today.year = 2007;
cout << “ Today’s date is = ” << today.day << “/”; cout << today.month <<
“/” << today.year << endl; return 0;
}
Output of the above program

Today’s date is = 10/5/2007

While the keyword public is not used to define the members of a class,
the C++ compiler assumes, by default, that all its members are private. The
data members are not accessible outside the class. For example, the
following program demonstrates the accessibility of the members.

Program 6

#include<iostream>
using namespace std;
int main()
{
class date { // by default, members are
private int day,month,year;
};
class date
today;
today.day = 10;

today.month = 5;
today.year = 2007;

cout << “ Today’s date is = ” << today.day


<< “/”; cout << today.month << “/”
<<today.year << endl;
}
The following error message will be displayed during the compilation time.
date.day is a
private date.month
is a private
date.year is a
private

A program to demonstrate how to define both data member and member function of a class within the
scope of class definition.

The OMT of a class date is given in Fig. 10.6.

Fig. 10.6 OMT of a Class Date

Program 7

#include<iostream>

using namespace std;


class date {
private :
int day, month, year;
public :
void getdata()
{
cout << “ enter the date ( dd-mm-year) ” << endl; cin >> day >> month >>
year;
}
void display ()
{
cout << “ Today’s date is = ” << day << “/”; cout << month << “/” << year
<< endl;
}
}; // end of class defnition

int main()
{

date today;
today.getdata();
today.display();
}
Program 8

//class with data and member function


#include <iostream>
using namespace std;
class date {
private :
int day,month,year; public :
void getdata( int d,int m,int y)
{
day = d; month = m; year = y;
}
void display (void)
{
cout << “ Today’s date is = ” << day << “/”; cout << month << “/” << year
<< endl;
}
Void main{
Date db;
db.

Output of the above program


Today’s date is = 10/5/2007
PROGRAM 9
WAP to add 2 numbers using scope resolution operators
class sample {
private : int x, int y;
public : int sum();
int diff();
};
int sample :: sum()
{
return(x+y);
}
int sample ::diff()
{
return(x-y);
}
int main()
{
sample obj1;
obj1.sum();
obj1.diff();
}
PROGRAM 10
WAP to add, subtract,, divide,mutiply 2 numbers using scope resolution
operators
#include <iostream>
using namespace std;
class sample {
private : int x, int y;
public : void getinfo();
void display();
int sum();
int diff();
int mult();
float div();
}; // end of class
void sample :: getinfo()
{
cout << “ enter any two number ? ” << endl;
cin >> x >> y ;
}
void sample :: display()
{
cout << “ x = ” << x << endl;
cout << “ y = ” << y << endl;
cout << “ sum = ” << sum() << endl;
cout << “ dif = ” << diff() << endl;
cout << “ mul = ” << mult() << endl;
cout << “ div = ” << div() << endl;
}
int sample :: sum()
{
return(x+y);
}
int sample :: diff()
{
return(x-y);
}
int sample :: mult()
{
return(x*y);
}
float sample :: div()
{
return(x/y);
}

int main()
{
sample obj1;
obj1.getinfo();
obj1.display();
obj1.sum();
obj1.diff();
obj1.mult();
obj1.div();
return 0;
}
Program 11 WAP of Dereferencing Operator
#include <iostream.h>
using namespace std;
int main()
{
int a = 10, b = 20;
int* pt;
pt = &a;
cout << "The address where a is stored is: " << pt <<
endl;
cout << "The value stored at the address by
dereferencing the pointer is: << *pt << endl;
}
Output
The address where a is stored is: 0x7ffdcae26a0c
The value stored at the address by dereferencing the
pointer is: 10
Program 12: We Can Directly assign value to a
pointer using Dereference :
#include <iostream>
using namespace std;
int main() {
int *p;
*p = 80; //Assiging value to pointer p by using
asterisk
cout << *p<<endl;
return 0;
}
Output
80
In Above code we directly assign value to pointer p
using asterisk (*) and using dereference we print its
pointed value that is 80.

Changing the value of the Pointer


There are 2 methods to change the value of pointers
 Change the address of the stored variable
 Change the value in the stored variable
When do you want to assign another address to the
pointer?
One of the functionalities of the pointer is that it can
store some other variable address. If we want to change
the address to another variable in that case we can just
reference the variable to another variable.

Program 13
// C++ Program to changing the address pointed by
pointer
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 20;
int* pt;
// Referencing to the pointer
pt = &a;
cout << "The address where a is stored is: " << pt <<
endl;

// dereference the pointer


cout << "The value stored at the address by
dereferencing the pointer is: "
<< *pt << endl
// Referening the address to same pointer
pt = &b;
// dereference the pointer
cout << "Pointer is now pointing at: " << pt << endl;
cout << "New value the pointer is pointing to is: " <<
*pt << endl;
return 0;
}
Output
The address where a is stored is: 0x7ffe14972128
The value stored at the address by dereferencing the
pointer is: 10
Pointer is now pointing at: 0x7ffe1497212c
New value the pointer is pointing to is: 20

Program 14
A program to demonstrate the concept of arrays as
class members
Example:
#include<iostream>

const int size=5;

class student

{
private:
int roll_no;
int marks[size];
public:
void getdata ();
void tot_marks ();
};

void student :: getdata ()


{
cout<<"\nEnter roll no: ";
cin>>roll_no;
cout<<"\nEntered roll no is: "<<roll_no;
for(int i=0; i<size; i++)
{
cout<<"Enter marks in subject"<<(i+1)<<": ";
cin>>marks[i] ;
}
}
void student :: tot_marks() //calculating total marks
{
int total=0;
for(int i=0; i<size; i++)
total+ = marks[i];
//total = marks[i]+total;
cout<<"\n\nTotal marks "<<total;
}
void main()
{
student stu;
stu.getdata() ;
stu.tot_marks() ;
}
Output:
Enter roll no: 101
Enter marks in subject 1: 67
Enter marks in subject 2 : 54
Enter marks in subject 3 : 68
Enter marks in subject 4 : 72
Enter marks in subject 5 : 82
Total marks = 343
Program 15
WAP in C++ and ask for the age of 5 students and find
the average age of the students.(declare data
member as private and use scope resolution
operator.)
Program 16
A program to demonstrate the concept of arrays as
class members
Example:
#include<iostream>

const int size=5;

class student

{
private:
int roll_no;
int marks[size];
public:
void getdata ();
void tot_marks ();
};

void student :: getdata ()


{
cout<<"\nEnter roll no: ";
cin>>roll_no;
cout<<"\nEntered roll no is: "<<roll_no;
for(int i=0; i<size; i++)
{
cout<<"Enter marks in subject"<<(i+1)<<": ";
cin>>marks[i] ;
}
}
void student :: tot_marks() //calculating total marks
{
int total=0;
for(int i=0; i<size; i++)
total+ = marks[i];
//total = marks[i]+total;
cout<<"\n\nTotal marks "<<total;
}
void main()
{
student stu;
stu.getdata() ;
stu.tot_marks() ;
}
Output:
Enter roll no: 101
Enter marks in subject 1: 67
Enter marks in subject 2 : 54
Enter marks in subject 3 : 68
Enter marks in subject 4 : 72
Enter marks in subject 5 : 82
Total marks = 343

Program 17
// C++ program that declare array as
data members
#include<iostream>
using namespace std;
class Employee
{
int id;
char name[30];
public:
void getdata();//Declaration of
function
void putdata();//Declaration of
function
};
void Employee::getdata(){//Defining of
function
cout<<"Enter Id : ";
cin>>id;
cout<<"Enter Name : ";
cin>>name;
}
void Employee::putdata(){//Defining of
function
cout<<id<<" ";
cout<<name<<" ";
cout<<endl;
}
int main(){
Employee emp; //One member
emp.getdata();//Accessing the
function
emp.putdata();//Accessing the
function
return 0;

}
Program 18
// C++ program that declare array as
object.
#include<iostream>
using namespace std;

class Employee
{
int id;
string name[30];
public:
void getdata();
void putdata();
};
void Employee::getdata()
{
cout << "Enter Id : ";
cin >> id;
cout << "Enter Name : ";
cin >> name;
}
void Employee::putdata()
{
cout << id << " ";
cout << name << " ";
cout << endl;
}
int main()
{
Employee emp[30];
int n, i;
cout << "Enter Number of Employees
- ";
cin >> n;
for(i = 0; i < n; i++)
emp[i].getdata();

cout << "Employee Data - " << endl;

// Accessing the function


for(i = 0; i < n; i++)
emp[i].putdata();
}
Program 19

A program that demonstrates the static data


members in C++ is given as follows −
#include <iostream>
#include<string.h>

using namespace std;


class Student {
private:
int rollNo;
char name[10];
int marks;
public:
static int objectCount;
Student() {
objectCount++;
}

void getdata() {
cout << "Enter roll number:
"<<endl;
cin >> rollNo;
cout << "Enter name: "<<endl;
cin >> name;
cout << "Enter marks: "<<endl;
cin >> marks;
}

void putdata() {
cout<<"Roll Number = "<< rollNo
<<endl;
cout<<"Name = "<< name <<endl;
cout<<"Marks = "<< marks
<<endl;
cout<<endl;
}
};
int Student::objectCount = 0;
int main(void) {
Student s1;
s1.getdata();
s1.putdata();

Student s2;
s2.getdata();
s2.putdata();

Student s3;
s3.getdata();
s3.putdata();
cout << "Total objects created = "
<< Student::objectCount << endl;
return 0;
}

Output
The output of the above program is as
follows −
Enter roll number: 1
Enter name: Mark
Enter marks: 78
Roll Number = 1
Name = Mark
Marks = 78

Enter roll number: 2


Enter name: Nancy
Enter marks: 55
Roll Number = 2
Name = Nancy
Marks = 55

Enter roll number: 3


Enter name: Susan
Enter marks: 90
Roll Number = 3
Name = Susan
Marks = 90
Total objects created = 3

Program 20
// C++ program to show passing of objects to a
function

#include <iostream>
using namespace std;
class Demo {
private:
int a;
public:
void set(int x)
{
a = x;
}
void sum(Demo ob1, Demo ob2)
{
a = ob1.a + ob2.a;
}

void print()
{
cout << "Value of A : " << a << endl;
}
};
int main()
{
//object declarations
Demo d1;
Demo d2;
Demo d3;
//assigning values to the data member of objects
d1.set(10);
d2.set(20);
//passing object d1 and d2
d3.sum(d1, d2);
//printing the values
d1.print();
d2.print();
d3.print();

return 0;
}
Output
Value of A : 10
Value of A : 20
Value of A : 30
Program 21 :defining the constructor within the class
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student()
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}

void display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
};

int main()
{
student s;

//constructor gets called automatically when we create the


object of the class
s.display();
return 0;

}
Program 22:// Cpp program to illustrate the concept of
Constructors
#include <iostream>
using namespace std;

class construct {
public:
int a, b;

// Default Constructor
construct()
{
a = 10;
b = 20;
cout<<a<<b;
}
};

int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl << "b: " << c.b;
return 0;
}

Program 23 / /CPP program to illustrate parameterized


constructors
#include <iostream>
using namespace std;
class Point {
private:
int x, y;

public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}

int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << p1.getX()<< ", p1.y = " << p1.getY();
return 0;
}
Program 24: // C++ Program to demonstrate the copy
constructor
#include <iostream>
#include <string.h>
using namespace std;

class student {
int rno;
string name;
double fee;

public:
student(int, string, double);
student(student & t) // copy constructor
{
rno = t.rno;
name = t.name;
fee = t.fee;
}
void display();
};
Student :: student(int no, string n, double f)
{
rno = no;
name = n;
fee = f;
}
void student :: display()
{
cout << endl << rno << "\t" << name << "\t" << fee;
}
int main()
{
student s (1001, "Ram", 10000);
s.display();
student obj (s); // copy constructor called
obj.display();
return 0;
}

Program 25
// C++ program to demonstrate the execution of constructor
// and destructor

#include <iostream>
using namespace std;

class Test {
public:
Test()
{ cout << "\n abc………….";
}
// Destructor
~ Test()
{ cout << "\nDestructor executed";
}
};
main()
{
Test t;
return 0;
}
Program 26
//C++ program to demonstrate the execution of constructor
and destructor when multiple objects are created

#include <iostream>
using namespace std;
class Test {
public:
// User-Defined Constructor
Test()
{ cout << "\n Constructor executed";
}

// User-Defined Destructor
~Test()
{ cout << "\n Destructor executed";
}
};

main()
{
// Create multiple objects of the Test class
Test t, t1, t2, t3;
return 0;
}

Program 27
A program to access the private data of a
class by non-member function through
friend function where the friend function
is declared in the location of public
category.

#include<iostream>
using namespace std;

class sample{
private : int x;
public : void getdata();
friend void display(class sample);
};
void sample :: getdata(){
cout<<"Enter a value for x\n*";
cin>>x;
}
void display(class sample abc)
{
cout<<"Entered number is : "<<abc.x;
cout<<endl;
}
int main()
{
sample obj;
obj.getdata();
cout<<"accessing the private data by
non-member ";
cout<<"function \n";
display (obj);
return 0;
}
Program 28
A program to access the private data of a
class by non-member function through
friend function where the friend function
is declared in the location of private
category.
//declaring friend function
#include<iostream>
using namespace std;

class sample{
private : int x;
friend void display(class sample);
public : void getdata();

};

void sample :: getdata(){


cout<<"Enter a value for x/n*";
cin>>x;
}
void display(class sample abc){
cout<<"Entered number is : "<<abc.x;
cout<<endl;
}
int main()
{
sample obj;
obj.getdata();
cout<<"accessing the private data by
non-member ";
cout<<"function \n";
display (obj);
return 0;
}
Program 30

A program to calculate the sum of private


data of the class first with a private
data of another class second through the
common friend function.

#include<iostream>
using namespace std;
class second; //forward declaration

class first{
private : int x;
public : inline void getdata();
inline void display();
friend int sum(first, second);
};
class second{
private : int y;
public : inline void getdata();
inline void display();
friend int sum (first, second);
};
inline void first :: getdata(){
cout<<"Enter a value for x\n";
cin>>x;
}
inline void second :: getdata(){
cout<<"Enter a value for y\n";
cin>>y;
}
inline void first :: display()
{
cout<<"Entered number is (x) = ";
cout<<x<<endl;
}
inline void second :: display(){
cout<<"Entered number is (y) = ";
cout<<y<<endl;
}
int sum(first one, second two){
int temp;
temp = one.x + two.y;
return(temp);
}
int main()
{
first a;
second b;
a.getdata();
b.getdata();
a.display();
b.display();
int te = sum(a,b);
cout<<"sum of the two private data
variables (x+y)";
cout<<"sum of the two private data
variables (x + y)";
cout<<" = "<<te<<endl;
return 0;
}
Unit 4
Program 31 C++ program to illustrate Base &
Derived Class
#include <iostream>
using namespace std;
// Declare Base Class
class Base {
public:
int a;
};

// Declare Derived Class


class Derived : public Base
{
public:
int b;
};
int main()
{
// Initialise a Derived class
Derived obj;
// Assign value to Derived class variable
obj.b = 3;
// Assign value to Base class variable via
derived class
obj.a = 4;
cout << "Value from derived class: "<< obj.b <<
endl;
cout << "Value from base class: " << obj.a <<
endl;
return 0;
}
Output:
Value from derived class: 3
Value from base class: 4

Program 32
WAP to find the sum of 2 numbers using single
inheritance.

Program 33
WAP to demonstrate how a class can be inherit
Privetely.
#include <iostream>
using namespace std;
class Person {
public: int id;
string name ;
public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}
};
class Student : private Person {
string course;
int fee;
public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}

void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};

int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}

Program 34
WAP to find the average of marks of 2 students
by inheriting the classclass can be inherit
Privetely.This is a program of single inheritance.

Program 34
WAP to display name,is salary of an employee
by single inheritance and the class can be inherit
Privetely.This is a program of single inhertance
Program 35
WAP to find the product of 3 numbers and the
class can be inherit Publicly.This is a program of
multilevel inhertance
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
void set_A()
{
cout<<"Enter the Value of A=";
cin>>a;
}
void disp_A()
{
cout<<endl<<"Value of
A="<<a;
}
};

class B: public A
{
private: int b;
public: void set_B()
{
cout<<"Enter the Value of B=";
cin>>b;
}

void disp_B()
{
cout<<endl<<"Value of B="<<b;
}
};

class C: public B
{
int c, p;
public:
void set_C()
{
cout<<"Enter the Value of C=";
cin>>c;
}

void disp_C()
{
cout<<endl<<"Value of C="<<c;
}

void cal_product()
{
p=a*b*c;
cout<<endl<<"Product of "<<a<<" *
"<<b<<" * "<<c<<" = "<<p;
}
};

main()
{
C objc ;
objc.set_A();
objc.set_B();
objc.set_C();
objc .disp_A();
objc. disp_B();
objc.disp_C();
objc.cal_product();

return 0;

Program 36
WAP to find the average of 5 numbers and the
class can be inherit Publicly.This is a program of
multilevel inheritance

Program 37
WAP to find the age of n students and it’s average
also ask marks of n students find average marks
and the class can be inherit Publicly.This is a
program of multiple inheritance

Program 38

program to print the average marks of six subjects using


the multiple Inheritance in the C++ programming
language.

#include <iostream>
using namespace std;

// create base class1


class student_detail
{
private:
int rno, sum = 0, i, marks[5];
public:
void detail()
{
cout << " Enter the Roll No: " << endl;
cin >> rno;
cout << " Enter the marks of five subjects " <<
endl;
for (i = 0; i < 5; i++)
{
cin >> marks[i];
}
for ( i = 0; i < 5; i++)
{
sum = sum + marks[i];
}
}

};

// create base class2


class sports_mark
{
private:
int s_mark;
public:
void get_mark()
{
cout << "\n Enter the sports mark: ";
cin >> s_mark;
}
};

class result: public student_detail, public sports_mark


{
int tot, avg;
public:

// create member function of child class


void disp ()
{
tot = sum + s_mark;
avg = tot / 6; // total marks of six subject / 6
cout << " \n \n \t Roll No: " << rno << " \n \t
Total: " << tot << endl;
cout << " \n \t Average Marks: " << avg;
}
};

int main ()
{
result obj; // create an object of the derived class

// call the member function of the base class


obj.detail();
obj.get_mark();
obj.disp();
}
Program 39

// C++ program for Hierarchical Inheritance


#include<iostream>
using namespace std;

class A
{
public:
void show_A() {
cout<<"class A"<<endl;
}
};
class B : public A
{
public:
void show_B() {
cout<<"class B"<<endl;
}
};

class C : public A
{
public:
void show_C() {
cout<<"class C"<<endl;
}
};

int main() {
B b; // b is object of class B
cout<<"calling from B: "<<endl;
b.show_B();
b.show_A();

C c; // c is object of class C
cout<<"calling from C: "<<endl;
c.show_C();
c.show_A();
return 0;
}
Program 40
Wap of Hierarchical inheritance to show a customer
bought 5 items from the grocery shop and 3 vegetable
from the vendor, find the total cost of items the
customer bought.

Program 41
// C++ program to illustrate the hybrid inheritance
Using Multilevel Inheritance and Hierarchical
Inheritance
#include <iostream>
using namespace std;

// Base class 1
class Meal {
public:
void print()
{
cout << "Different types of meals are served :"
<< endl;
}
};

// Derived class 1 from Meal (Hierarchical Inheritance)


class Breakfast : public Meal {
public:
void print()
{
cout << "\nBreakfast is a type of meal." <<
endl;
}
};

// Derived class from breakfast (Multilevel Inheritance)


class Milk : public Breakfast {
public:
void print()
{
cout << "Milk served in breakfast." << endl;
}
};

// Derived class from Milk (Multilevel Inheritance)


class Yoghurt : public Milk {
public:
void print()
{
cout << "Yoghurt made from milk." << endl;
}
};

// Derived class 2 from Meal (Hierarchical Inheritance)


class Dessert : public Meal {
public:
void print()
{
cout << "\nDessert is a type of meal." << endl;
}
};

// Derived class from Dessert (Multilevel Inheritance)


class Sweets : public Dessert {
public:
void print()
{
cout << "Sweets served in Dessert." << endl;
}
};

// Derived class from Sweets (Multilevel Inheritance)


class Pastry : public Sweets {
public:
void print()
{
cout << "Pastry is a type of sweet." << endl;
}
};

int main()
{
Meal types;
Breakfast servedBreakfast;
Milk milk;
Yoghurt yoghurt;
Dessert servedDessert;
Sweets s;
Pastry pastry;

types.print();
servedBreakfast.print();
milk.print();
yoghurt.print();
servedDessert.print();
sweet.print();
pastry.print();

return 0;
}

Program 42

WAP To show the need of Virtual Base Class in C++


#include <iostream>
using namespace std;

class A {
public:
void show()
{
cout << "Peter\n";
}
};

class B : public A {
};
class C : public A {
};

class D : public B, public C {


};

int main()
{
D object;
object.show();
}
Compile Errors:
prog.cpp: In function 'int main()':
prog.cpp: error: request for member 'show' is
ambiguous
object.show();
^
prog.cpp: note: candidates are: void A::show()
void show()
^
Program 43 Program to Demonstrate Virtual Function
#include <iostream>
using namespace std;
class A {
public:
int a;
show()
{
a = 10;
}
};
class B : public virtual A
{
};

class C : public virtual A


{
};

class D : public B, public C


{
};

int main()
{
D object;
cout << "a = " << object.show() << endl;

return 0;
}
Output
a = 10
Program 44
// C++ program to demonstrate function overloading or
Compile-time Polymorphism
#include <iostream>
using namespace std;
class Demo {
public:
void func(int x)
{
cout << "value of x is " << x << endl;
}

void func(double x)
{
cout << "value of x is " << x << endl;
}

void func(int x, int y)


{
cout << "value of x and y is " << x << ", " << y
<< endl;
}
};

int main()
{
Demo obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85, 64);
return 0;
}

Output
value of x is 7
value of x is 9.132
value of x and y is 85, 64

Program 45
WAP To achieve compile time polymorphism via
function overloading and display your name,
rollno, jluid and course.

Program 46

WAP IN C++ program to illustrate concept of Virtual


Functions and achieve runtime polymorphism.

#include <iostream>
using namespace std;
class base {
public:
virtual void print()
{ cout << "print base class\n";
}

void show()
{ cout << "show base class\n";
}
};
class derived : public base
{
public:
void print()
{ cout << "print derived class\n";
}

void show()
{ cout << "show derived class\n";
}
};

int main()
{
Base * bptr;
derived d;
bptr = &d;

// Virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();

return 0;
}

Output
print derived class
show base class

Program 47
WAP to demonstrate the setw & endl
manipulators.

Program 48
WAP to demonstrate the setfill manipulators.
Program 49
WAP to demonstrate the setprecision
manipulators.

Program 50

WAP to create a design using setw and set


precision manipulators.

Program 51
WAP TO read the line from keyboard and display
the function as array to the screen using cin.get
function.

You might also like