0% found this document useful (0 votes)
2 views

c++ assignment no.4-1

The document contains multiple C++ programs demonstrating various concepts of polymorphism, including compile-time polymorphism through function and operator overloading, as well as run-time polymorphism using virtual functions. Each program is accompanied by code snippets and their respective outputs. Additionally, it features examples of using the 'this' pointer and pure virtual functions.

Uploaded by

uiuxnihal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c++ assignment no.4-1

The document contains multiple C++ programs demonstrating various concepts of polymorphism, including compile-time polymorphism through function and operator overloading, as well as run-time polymorphism using virtual functions. Each program is accompanied by code snippets and their respective outputs. Additionally, it features examples of using the 'this' pointer and pure virtual functions.

Uploaded by

uiuxnihal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 8

-------------------------------------------------------------------------------------------------------

Program No : 01 A
Name : Ghanashyam Basaveshwar Kumbhar.
Class :BCA-II. Roll No: 59
Program Name: : Write a C ++ program to demonstrate compile time polymorphism
using:- A) function overloading
B) operator overloading
-------------------------------------------------------------------------------------------------------
Program Code:

A) Function overloading :-

#include<iostream>
using namespace std;
class Function{
public:
void fun(int x){
cout<<"This value is Integer... "<<x<<endl;
}
void fun(string x){
cout<<"This value is String..."<<x<<endl;
}
};
int main(){
Function F;
F.fun(07);
F.fun("WhyShyam");

return 0;
}

/*Output:

jcj-bca-pc24@jcjbcapc24:~/whyshyam$ gedit funover.cpp


jcj-bca-pc24@jcjbcapc24:~/whyshyam$ g++ -o funover funover.cpp
jcj-bca-pc24@jcjbcapc24:~/whyshyam$ ./funover
This value is Integer... 7
This value is String...WhyShyam

*/
-------------------------------------------------------------------------------------------------------
Program No : 01 A
Name : Pavan Dnyaneshwar Mali.
Class :BCA-II. Roll No: 60
Program Name: : Write a C ++ program to demonstrate compile time polymorphism
using:- A) function overloading
B) operator overloading
-------------------------------------------------------------------------------------------------------
Program Code:

A) Function overloading :-

#include<iostream>
using namespace std;
class Function{
public:
void fun(int x){
cout<<"This value is Integer... "<<x<<endl;
}
void fun(string x){
cout<<"This value is String..."<<x<<endl;
}
};
int main(){
Function F;
F.fun(07);
F.fun("WhyShyam");

return 0;
}

/*Output:

jcj-bca-pc24@jcjbcapc24:~/whyshyam$ gedit funover.cpp


jcj-bca-pc24@jcjbcapc24:~/whyshyam$ g++ -o funover funover.cpp
jcj-bca-pc24@jcjbcapc24:~/whyshyam$ ./funover
This value is Integer... 7
This value is String...WhyShyam

*/
-------------------------------------------------------------------------------------------------------
Program No : 01 B
Name : Ghanashyam Basaveshwar Kumbhar.
Class :BCA-II. Roll No: 59
Program Name: Write a C ++ program to demonstrate compile time polymorphism
using:- a) function overloading
b) operator overloading
-------------------------------------------------------------------------------------------------------
Program Code:

B) Operator overloading :-

#include<iostream>
using namespace std;
class Test{
private:
int num;
public:
Test():num(8){

}
void operator ++(){
num=num+2;
}
void print(){
cout<<"The count is : "<<num;
}
};
int main() {
Test t;
++t;
t.print();
return 0;
}

/*Output:

jcj-bca-pc24@jcjbcapc24:~/whyshyam$ gedit Opover.cpp


jcj-bca-pc24@jcjbcapc24:~/whyshyam$ g++ -o Opover Opover.cpp
jcj-bca-pc24@jcjbcapc24:~/whyshyam$ ./Opover
The count is : 10

*/
-------------------------------------------------------------------------------------------------------
Program No : 02
Name : Ghanashyam Basaveshwar Kumbhar.
Class :BCA-II. Roll No: 59
Program Name: Write a C ++ program to demonstrate run time polymorphism using
virtual function.
-------------------------------------------------------------------------------------------------------

Program Code:

#include<iostream>
using namespace std;
class FC{
public:
virtual void display(){
cout<<"Which club is best?"<<endl;
}
};
class RM : public FC{
void display(){
cout<<"Real Madrid Is The Best Club In The World...";
}
};
int main(){
FC *f;
RM r;
f=&r;
f ->display();

return 0;
}

/*Output:

jcj-bca-pc24@jcjbcapc24:~/whyshyam$ gedit runtime.cpp


jcj-bca-pc24@jcjbcapc24:~/whyshyam$ g++ -o runtime runtime.cpp
jcj-bca-pc24@jcjbcapc24:~/whyshyam$ ./runtime
Real Madrid Is The Best Club In The World...

*/
-------------------------------------------------------------------------------------------------------
Program No : 03
Name : Ghanashyam Basaveshwar Kumbhar.
Class :BCA-II. Roll No: 59
Program Name: Write a C ++ program to demonstrate this pointer.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class Student{
private:
int Id;
string Name;
string Class;
public:
Student(int i,string n,string c){
Id = i;
Name = n;
Class = c;
}
void set(int i,string n,string c){
this->Id;
this->Name;
this->Class;
}
void display(){
cout<<"Student ID:"<<this->Id<<endl;
cout<<"Student NAME:"<<this->Name<<endl;
cout<<"Student CLASS:"<<this->Class<<endl;
cout<<"----------------------------------"<<endl;
}
};
int main(){
Student S(01,"Whyshyam","BCA-I");
Student S1(02,"Stillshyam","BCA-II");
Student S2(03,"Shyam","BCA-III");

S.display();
S1.display();
S2.display();

return 0;
}
/*Output:
jcj-bca-pc24@jcjbcapc24:~/whyshyam$ gedit thispointer.cpp
jcj-bca-pc24@jcjbcapc24:~/whyshyam$ g++ -o thispointer thispointer.cpp
jcj-bca-pc24@jcjbcapc24:~/whyshyam$ ./thispointer
Student ID:1
Student NAME:Whyshyam
Student CLASS:BCA-I
----------------------------------
Student ID:2
Student NAME:Stillshyam
Student CLASS:BCA-II
----------------------------------
Student ID:3
Student NAME:Shyam
Student CLASS:BCA-III
----------------------------------
*/
-------------------------------------------------------------------------------------------------------
Program No : 04
Name : Ghanashyam Basaveshwar Kumbhar.
Class :BCA-II. Roll No: 59
Program Name: Write a C ++ program to demonstrate pure virtual function.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class Shape{
public:
virtual void draw()=0;
};
class Circle:public Shape{
public:
void draw(){
cout<<"Drawing Circle....";
}
};
int main(){
Circle c;
c.draw();
return 0;
}

/*Output:

jcj-bca-pc24@jcjbcapc24:~/whyshyam$ gedit thispointer.cpp


jcj-bca-pc24@jcjbcapc24:~/whyshyam$ gedit virtual.cpp
jcj-bca-pc24@jcjbcapc24:~/whyshyam$ g++ -o virtual virtual.cpp
jcj-bca-pc24@jcjbcapc24:~/whyshyam$ ./virtual
Drawing Circle....

*/

You might also like