"pure virtual function in c++" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "pure virtual function in c++" answered properly. Developers are finding an appropriate answer about pure virtual function in c++ related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like pure virtual function in c++. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on pure virtual function in c++. 

pure virtual function in c++

By Alive AngelfishAlive Angelfish on Aug 05, 2020
#include <iostream>
#include  <string>
//Pure virtual function  or inteface allows us to define a function in a base class that doesn't have an implementation or definition in the base class and force sub classes to implement that function
//Pure virtual function is also called an interface in other languages
class Entity {
public:
	//virtual std::string GetName() { return "Entity"; }//This is a function that is just virtual .Overriding this function in sub class is optional we can instantiate subcllass without overriding  or implementing this function
	
	//Below is an example a Pure Virtual Function
	//It is an unimplemented function ant it forces the  sub class to implement it and define it
	//You will not be able to instantiate sub class without implementing or defining the function in sub class
	virtual std::string GetName() = 0; 
  //the pure virtual function must have virtual written at the beginning and =0 at the end
 //This function cannot contain any definition in base class,it is just a declaration
};
class Player :public Entity {
	std::string m_name;

public:
	Player(const std::string& name)
		:m_name(name)
	{};
	void Print() { std::cout << "This is Sub class" << std::endl; };
	std::string GetName()override { return m_name; };//Pure virtual functions is implemented here in this sub class
};
void PrintName(Entity* entity) {

	std::cout << entity->GetName() << std::endl;
}
int main()
{
	//Entity a;//We can't do this because class Entity contains function that is unimplemented
	Player x("Jacob");//This will work because we have implemented or defined the function in this sub class
	std::cin.get();
}

Add Comment

9

what is abstract class in c++

By Cheerful CrabCheerful Crab on Nov 05, 2020
//Code by Soumyadeep Ghosh 
//insta : @soumyadepp
//linked in : https://www.linkedin.com/in/soumyadeep-ghosh-90a1951b6/
#include <bits/stdc++.h>

using namespace std;

class person
{
  string p_id;
  public:
  virtual void get_info()=0; //declaring person as abstract class
  virtual void show()=0;
};

class student:public person
{
  string name;
  int roll_no;
  public:
  /*overriding the pure virtual function declared in base class otherwise
    this class will become an abstract one and then objects cannot be created
    for the same*/
    void get_info()
    {
      cout<<"Enter name of the student "<<endl;
      cin>>name;
      cout<<"Enter roll number of the student "<<endl;
      cin>>roll_no;
    }
   void show()
   {
     cout<<"Name : "<<name<<" Roll number: "<<roll_no<<endl;
   }
};

int main()
{
  person *p;
  p=new student;
  p->get_info();
  p->show();
  return 0;
}

Add Comment

2

virtual function in c++

By Alive AngelfishAlive Angelfish on Aug 04, 2020
#include <iostream>
#include<string>
	//Virtual Functions are functions that allow us to override methods in subclasses
//In this example  we have an entity class as a base class and class player inherits from public entity 
class Entity {
public:
	virtual std::string GetName() { return "Entity"; }//It is a method in base class that we want to modify in sub class Player
	void Print() { std::cout << "This is Base class" << std::endl;}//function that is not virtual
};
class Player :public Entity {
	std::string m_name;

public:
	Player(const std::string& name)
		:m_name(name)
	{};
	void Print() { std::cout << "This is Sub class" << std::endl; };//function that is not virtual
	std::string GetName()override { return m_name; };//overriding the function in sub class
};

int main()
{
	Entity* e = new Entity();
	std::cout << e->GetName() << std::endl;
	Player* p = new Player("Jacob");
	std::cout << p->GetName() << std::endl;
	PrintName(p);// This function calls the GetName method from the Player instance despite it takes an entity instance as a parameter this is because player class is a sub  class of Entity and the method is marked virtual it will map with the method in the Player class and call it from there .It outputs => Jacob
	//if It was not virtual it would have called The method From Entity Instance and output would be => Entity
	Entity* notvirtualentity = new Entity();
	Player* notvirtualpalyer = new Player("XX");
	notvirtualentity =  notvirtualpalyer;
	notvirtualentity->Print();//It prints => this is base class if it was virtual function it would call print function from Player Class and print => This is subclass
	std::cin.get();
}

Add Comment

1

virtual function c++

By Glorious GorillaGlorious Gorilla on Sep 22, 2020
#include <iostream>
#include <string>

class Entity {
public:
  virtual std::string getName();
  void print(); 
};

virtual std::string Entity::getName() {
	return "Entity";
}

void Entity::print() {
	std::cout << "This is the base class" << std::endl;
}

class Player : public Entity {
  std::string m_name;
public:
	Player(const std::string& name): m_name(name) {};
  	void print();
  	virtual std::string getName();
};

virtual std::string Player::getName() {
	return m_name;
}

void Player::print() {
	std::cout << "This is the sub class" << std::endl;
}

int main() {
	Entity* e = new Entity();
  	std::cout << e->getName() << std::endl;
  	Player* p = new Player("Jacob");
  	std::cout << p->getName() << std::endl;
  	p->print();
  	e->print();
  
  	Entity* notVirtualEntity = new Entity();
  	Player* notVirtualPlayer = new Player("Bob");
  	notVirtualEntity = notVirtualPlayer;
  	notVirtualEntity->print();
  	notVirtualEntity->getName();
}

Add Comment

-1

All those coders who are working on the C++ based application and are stuck on pure virtual function in c++ can get a collection of related answers to their query. Programmers need to enter their query on pure virtual function in c++ related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about pure virtual function in c++ for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of pure virtual function in c++ while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "pure virtual function in c++". Visit this developer's friendly online web community, CodeProZone, and get your queries like pure virtual function in c++ resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "pure virtual function in c++"

View All C++ queries

C++ queries related to "pure virtual function in c++"

pure virtual function in c++ virtual function in c++ virtual function c++ install virtual box kali how to use a non const function from a const function passing function to another function in c++ c++ convert template function to normal function The syntax to decexample of a function declarationlare a function is: pass a value to the function parameter while calling the function how to use python sleep function on c++ sine function in cpp find function in c++ how to use winmain function reverse string efficient in cpp without using function sum of 2 numbers in cpp function reference function in c++ c++ sleep function 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS time function c++ return array from function c++ c++ main function c++ sort function time complexity how to get name of caller function c++ c++ template function gcd function in c++ define my own compare function sort C++ stl c++ callback member function function call in c++ friend function in c++ how to declare a function in c++ z function cp algorithm factorial c++ without using function c++ function overload what is time complexity of swap function insert function in c++ vector function template How to make a function in C++ built in function in c++ for binary to decimal binary search function in c++ is it len function is aviable for c+= function in c++ function in struct c++ euler's totient function c++ stack function in cpp passing array to function c++ pointer sqrt() function in c++ how to return an array from a function count function c++ clear function in vector reverse string in c++ without using function next_permutation function in c++ c++ function default argument inline function in c++ standard deviation function in c++ what does compare function do in c++ sort function in cpp cpp lambda function how to modify 2d array in function c++ template function in C++ calling base class function from derived class object function to write a string in loercase in c++ c++ passing vector to function Function pointer C++ quick sort predefined function in c++ c++ round function function declerations in C++ c++ function to find length of array sleep system function linux c++ sort inbuilt function in c++ cpp function takes in vector friend function cpp reference thread c++ member function function for searching in map in c++ how to make a function in cpp how to declare function with multiple parameter c++ cpp function that returns two arguments C++ invalid use of 'this' outside of a non-static member function of c++ bind class member function & before function arg in cpp accepting multiple values from a function in cpp launch function with signal c++ function overriding in oop c++ c++ strict function return checking decimal to binary predefined function c++ check source code function return stl function to reverse an array map at function c++ statement that causes a function to end in c++ Write a function called clean that takes a C++ string as input and removes any characters in the string that are not letters except for space blanks. c++ check function with no return value Write a function called max_size that takes a vector of strings as an input and returns the string with the maximum length. sfml thread multi argument function sort using comparator anonymous function c++ c++ check function return value The syntax to declare a function is: QT error: invalid use of 'this' outside of a non-static member function function return floatin c++ wap in c++ to understand function template c++ function return pointer to itself call the above greet() function Function Template with multiple parameters c++ function return array how to define function prototypes in c++ passing 2d vector to function c++ put a function in a other thread how to set arrays as function parameters in c++ call function from separate bash script defining function in other file c++ how to import only one function function return with int c++ Function with Parameters lambda function qt connect extra parameter in comparator function for sort converting a string to lowercase inbuld function in cpp virual function inbuilt function for bin to dec in c++ return multiple objects from a function C++ using references how to write int menu () function in c++ gdb get return value of function what is require to run min max function on linux in cpp how to use getline function inc return array of string in function c++ return function in cpp swap function in cpp bool function in c++ c++ how to make function as argument c++ swap function I need to write an int function in which there are only cout statements and if I return 0/1 it prints them too. built oin function to get maximumof vector sort function c++ call python function arduino map function pow function c++

Browse Other Code Languages

CodeProZone