Significance of Virtual Function's in C++ - c++14

I am learning C++ and came around virtual functions. I got to know that virtual functions are the one declared in the base class and used in the derived class. On the basis of definition I tried to make two simple programs. Please see below :
This Program is using virtual keyword :
#include <iostream>
using namespace std;
class Name {
public :
Virtual void myName() {
cout << "My First Name is Varun" << endl ;
}
};
class LName: public Name{
public :
void myName() {
cout << "My Last Name is Srivastava" << endl;
}
};
int main() {
Name *name = new Name();
name -> myName();
LName *lname = new LName();
lname -> myName();
Name *nm;
LName ln;
nm = &ln;
nm -> myName();
ln.myName();
}
Output :
My First Name is Varun
My Last Name is Srivastava
My Last Name is Srivastava
My Last Name is Srivastava
Below example is without virtual keyword :
#include <iostream>
using namespace std;
class Name {
public :
void myName() {
cout << "My First Name is Varun" << endl ;
}
};
class LName: public Name{
public :
void myName() {
cout << "My Last Name is Srivastava" << endl;
}
};
int main() {
Name *name = new Name();
name -> myName();
LName *lname = new LName();
lname -> myName();
Name *nm;
LName ln;
nm = &ln;
nm -> myName();
ln.myName();
}
Output :
My First Name is Varun
My Last Name is Srivastava
My First Name is Varun
My Last Name is Srivastava
Here I can see the difference, but I am not able to get the why there is difference in the output and what exactly is "virtual" keyword doing.

Related

copy constructor do not work in cpp template

I'm writing a test program about c++ type erasure, the code is put on the end.
when I run program , the test case 2 output as follow:
A default cstr...0x7ffe0fe5158f
obj_:0x7ffe0fe5158f objaaa 0x7ffe0fe5158f
Print A 0x7ffe0fe5158f
my machine: Linux x86-64, gcc 4.8
In my opinion, "Object obj2(a2);" makes a class Model by lvalue reference, so it should call A's copy constructor,
but actually it did not work, it makes me confused.
someone can give a explanation, thank you in advance.
the program is list as follow:
#include <memory>
#include <iostream>
class Object {
public:
template <typename T>
Object(T&& obj) : object_(std::make_shared<Model<T>>(std::forward<T>(obj))) {
}
void PrintName() {
object_->PrintName();
}
private:
class Concept {
public:
virtual void PrintName() = 0;
};
template <typename T>
class Model : public Concept {
public:
Model(T&& obj) : obj_(std::forward<T>(obj)) {
std::cout << "obj_:" << std::addressof(obj_) <<" objaaa " << std::addressof(obj) << std::endl;
}
void PrintName() {
obj_.PrintName();
}
private:
T obj_;
};
private:
std::shared_ptr<Concept> object_;
};
class A {
public:
A(A& a) {
std::cout<< "A copy cstr...a" << this << std::endl;
}
A(A&& a) {
std::cout << "A move cstr...." <<this<< std::endl;
}
A() {
std::cout << "A default cstr..." <<this<< std::endl;
}
void PrintName() {
std::cout << "Print A " << this << std::endl;
}
};
int main(void)
{
// test case 1
Object obj{A()};
obj.PrintName();
// test case 2
A a2;
Object obj2(a2);
obj2.PrintName();
return 0;
}
In Object obj2(a2);, no copy is made. T in the constructor of Object is deduced to be A&, so it instantiates Model<A&>, which stores a reference to the original a2 object as its obj_ member.
Observe that in your debug output, a2's constructor, Model's constructor and PrintName all print the same address. You can further confirm that this address is in fact &a2.

In-class initialization of data members. Will the value left after the inheritance?

So simple question is, if I have such situation:
class FooA{
public:
int getID(){
return ID;
}
private:
int generateID(){
//some code that generate unique ID
}
int ID = generateID();
};
class FooB : public FooA{
};
Will the ID initialized in the FooA goes also to the FooB? So for FooB I can use getID and for example get value from FooA::generateID.
Every instance of both classes will run generateID anew, producing unique value for every instance.
No, ID generated for an instance of parent class won't be same as ID generated for an instance of descendant, because they would have different instances.
Yes, descendant can use or expose parent's methods.
class FooA{
public:
int getID(){
return ID;
}
private:
int generateID(){
static int i = 0;
return i++;
}
int ID = generateID();
};
class FooB : public FooA{
};
int main() {
// your code goes here
cout << (FooA()).getID() << endl; // 0
cout << (FooA()).getID() << endl; // 1
cout << (FooB()).getID() << endl; // 2
return 0;
}
https://ideone.com/5Klf3d

How SC_CTOR works in SystemC

In SystemC module constructor can be defined using SC_CTOR macro:
#define SC_MODULE(user_module_name) \
struct user_module_name : ::sc_core::sc_module
#define SC_CTOR(user_module_name) \
typedef user_module_name SC_CURRENT_USER_MODULE; \
user_module_name( ::sc_core::sc_module_name )
Need to understand use scope specifier operator before sc_core.
As per the macro user can generate constructor but it takes argument sc_module_name.
User module struct user module is inheriting class sc_module, In class sc_module there are different overloaded constructor like
sc_module();
sc_module( const sc_module_name& nm ); /* for those used to old style */
/* DEPRECATED */ sc_module( const char* nm );
/* DEPRECATED */ sc_module( const std::string& nm );
As per SC_CTOR it is declaring any object of type sc_moduke_name in constructor body if expanded the constructor will take form user_module(::sc_core::sc_module) { /*constructor body*/}.
To understand simulate same code but failed to create object of type struct B
class mname {
private:
char *name;
public:
mname() {
cout << "mname constructor invoked\n";
cout << "\t name\t" << name;
}
};
class A {
friend class mname;
private:
mname * mn;
public:
A() {
cout << "A constructor called\n";
}
A(const mname& m) {
cout << "freind class constructor of A\n";
}
A(const std:: string& s) {
cout << "string type construfctor\n";
}
};
struct B: A {
B(mname ) {
cout << "struct b cosntructor called\n";
}
};
int main() {
B obj(mname);
return 0;
}
A typical code to create a module is :
SC_MODULE(my_module)
{
SC_CTOR(my_module)
{
}
};
With macros, this code will expand to:
struct my_module : ::sc_core::sc_module
{
typedef my_module SC_CURRENT_USER_MODULE;
my_module( ::sc_core::sc_module_name )
{
}
};
If you want to create an instance of this module, you need to pass an instance of ::sc_core::sc_module_name in the constructor and not the class name as you did in your example code. You can also pass something to create implicitly an instance of ::sc_core::sc_module_name as a const char* (cf ::sc_core::sc_module_name constructors)
my_module a("name_of_my_module");
or
::sc_core::sc_module_name module_name("another_name");
my_module b(module_name);
In your example code, just replace the main with :
int main() {
mname name; // instance of mname
B obj(name);
return 0;
}

Why is this code working in a different way than expected? I think its working right, is it?

Here is what I am supposed to do.
Here is what I did.(on codeblocks IDE with minGW compiler)
#include <iostream>
using namespace std;
class person
{
private:
string name;
int age;
double height;
double weight;
public:
void set_private_members(string n, int a, double h, double w)
{
name = n;
age = a;
weight = h;
height = w;
}
void print_private_members()
{
cout <<"The name of person is:- "<<name<<"\n The age of person is:- " <<age<<"\n The weight of person is:- "<<weight;
cout <<"\n The height of person is:- "<<height<<endl;
}
};
void modify_person(person);
int main()
{
person p;
p.set_private_members("Ayush",19,165.7,47.2);
cout <<"We are in main function right now \n";
p.print_private_members();
modify_person(p);
cout <<"We are now back in main function \n The values of object passed to modify_person function are as \n";
p.print_private_members();
return 0;
}
void modify_person(person z)
{
cout <<"We are in modify_person function \n Modifying person details... \n";
z.set_private_members("Priyanshi",15,159.1,50.6);
cout <<"The details are now as:- \n";
z.print_private_members();
}
Here is the output to this code
however the expected output is to have priyanshi details in the class object when print_private_members function is called in main() for the last time.
Who is wrong? Me or they? I think if I call modify_person by reference then the expected should come.

boost::bind - Getting write access to member variable? (Like boost::multi_index::member)

Just out of curiosity, is there a way to get write access to member variables via boost::bind? I can get it via boost::multi_index::member, but just want to know other methods as well.
Example:
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/multi_index/member.hpp>
using namespace std;
struct Test
{
string name;
Test(const string &name)
: name(name)
{ }
};
int main()
{
Test test("Bob");
boost::multi_index::member<Test, string, &Test::name> nameMember;
string &ref = nameMember(test);
cout << ref << "\n";
// Write Access
ref = "Tim";
// Read-only Access
boost::function<const string& (Test*)> nameGetter = boost::bind(&Test::name, _1);
cout << nameGetter(&test) << "\n";
return 0;
}
Output:
Bob
Tim
Yes, it is possible:
// Read-write Access
boost::function<string&(Test*)> nameSetter =
boost::bind<std::string&>(&Test::name, _1);
nameSetter(&test) = "test";
cout << ref << "\n";

Resources