Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to write a c++ program including:
1. virtual function
2.static member function
3.assignment operator
4.copy constructor
5.function overloading

how to approach this?

What I have tried:

I just wrote how basically everything is implemented, but that was wrong
Posted
Updated 6-Apr-18 0:15am
Comments
Rick York 5-Apr-18 10:23am    
One suggestion: you need to demonstrate function overloading and a virtual function so this implies you need a base class. Implement a simple base class and then derive a specialized class from it.

What I have tried is :
#include <iostream>
using namespace std;

class base{
int a,b;
public:
base(){
cout<<"defult constr"<<endl;
}
base(int a1,int b1){
a=a1;
b=b1;
}
virtual ~base(){
cout << "base destr"<<endl;
}
void show(int s){
cout << s << endl;
}
void show(double s){
cout << s << endl;
}
void print(){
cout<< a << " " << b << endl;
}
void operator =(const base &o){
a=o.a;
b=o.b;
}
base (const base &o){
a=o.a;
b=o.b;
}
virtual void fun(){
cout<< "base class "<<endl;
}
static void sfun(){
cout << "static function"<<endl;
}
};
class derv:public base{
public:
derv(){
cout<<"default constr"<<endl;
}
~derv(){
cout << "destr derv"<< endl;
}
void fun(){
cout<< "derv class "<<endl;
}
};

int main() {
base o(1,2);
base o1(3,4);
base :: sfun();
base *b1;
base b;
derv d;
b1=&d;
d.fun();
base o2=o;
o.print();
o1.print();
o2.print();
o1=o;
o1.print();
base ov;
ov.show(10);
ov.show(100.1234);
delete b1;
return 0;
}
 
Share this answer
 
v3
Comments
KarstenK 6-Apr-18 6:20am    
Gratulations, looks like you have done your homework. Tip: better naming and code aligments belong also to writing good code. ;-)
Anonygeeker 6-Apr-18 6:36am    
Is my answer correct?Or copy constructor I should implement using dynamic memory allocation?
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
For your homework it is important to understand these concepts of C++. I recommand this outstanding C++ tutorial site for your learning.

When you understand these concepts you will find some solution by writing some classes like that:
C++
class MyClass
{
  // 2. static member function
  static void myStaticfunction();
};
//Usage
MyClass::myStaticfunction();
 
Share this answer
 
Quote:
I just wrote how basically everything is implemented, but that was wrong

We can help you to fix your code, but you need to show that code and explain where you are stuck.

Otherwise, we do not your homework.
You can't learn those concepts by have us doing the homework.
Advice: reread your lessons, read C++ book , find a tutorial.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900