C++ :: Virtual Keyword In Inheritance?
Feb 19, 2014
How to put virtual keyword in the function of the base class. Will the function still be over-written properly? What will happen if I
(1) call function from base class pointer
(2) call function from derived class pointer
(3) call function from derived class object
If the virtual keyword is present, the over-written version will be called in all cases, am I mistaken?
View 1 Replies
ADVERTISEMENT
Jan 6, 2014
have a look at the following code :
class Base
{
public:
virtual ~Base()
{
cout << "Calling ~Base()" << endl;
[Code]...
Now this program produces the following result:
Calling ~Derived()
Calling ~Base()
i was reading online and got stuck here. i am unable to understand why 'calling ~Base()' is been printed here? when we reached delete pbase; in int main() it goes to Base class first and finds that its destructor is virtual so it goes to Derive class and finds another destructor and executes it but why does it prints ~Base() in any case?
View 4 Replies
View Related
Apr 10, 2013
I have the following classes and 'dreaded diamond':
A
/
/
B C
/
/
D
|
|
E
Classes B & C both inherit from A using public virtual A.
E is the only concrete class. None of the classes are totally abstract.
Every class has a copy constructor.
All of the copy constructors are chained together through the initialization lists.
E correctly calls D's copy constructor.
D correctly calls B and C's copy constructors.
But neither B nor C call A's copy constructor, although A's default constructor is called. To reiterate B and C have a call to A's copy constructor in their initialization lists.
I guess A's default constructor is being called is because of virtual inheritence, but why isn't its copy constructor called (too)?
A's copy constructor includes some very important code and I could do with calling it. Should I call it from the concrete class' initialization list or is that considered bad form?
View 8 Replies
View Related
Sep 30, 2014
I've got the following code which demonstrates a problem :
Code:
struct A {
double x[2];
double i;
};
struct B : virtual public A {
double y[2];
[code]....
I'm wondering if this is a compiler bug. Why doesn't the pointer-to-(derived)-member work for the array if it works for the non-array?
View 4 Replies
View Related
Jan 26, 2014
In c# extending the class and implementing an interface is done by : symbol.. But c# has extend keyword. What is the use of extend keyword in c#?
View 8 Replies
View Related
Dec 27, 2012
class Parent{
public:
virtual int width();
virtual int height();
int area(){return width()*height();};
[Code] ....
View 10 Replies
View Related
Jun 30, 2014
for starters i hope this question wasn't posted yet.i want to write a small program that askes the user what he wants to do , and then executes the comand in system . But for some reason it doesn't quit on the key word this is the code:
Code:
#include <stdio.h>
/*a simple interaction programme that schow the system options*/
#define systemt "x1b[32m" /*changes the color to green for system output*/
#define user "x1b[0m" /*changes the color to white for user input*/
int main() {
[Code]...
Is there something wrong white the code or does the system bock me from reusing the choise string. i'll inculde the original c file as well
View 1 Replies
View Related
Feb 14, 2014
One thing make me consider:
FistClass F1 = new FistClass ();
FistClass F2 = F1;
second line why i dont need to firstly create instance for F2 and then make reference betwwen to classes like:
FistClass F1 = new FistClass ();
FistClass F2 = new FistClass ();
F2 = F1;
View 9 Replies
View Related
Sep 24, 2014
I'm trying to read a C code, but there are functions including CLASS word at the titles of functions. Is it a correct implementation?
For example: double CLASS getreal (int type)
What is the meaning of CLASS in titles of functions in C? I could not find such an usage in my C book?
View 6 Replies
View Related
Aug 18, 2014
I was trying to make a tic tac toe game where the user can play against the computer
It s in borland c++ and not in c++ 11 so i wanted to convert it into c++11. My program shows error since there is nothing called conio.h in c++11. Hence I want to know if there is any alternate keyword gor the function getch() so that i could ask the user to press [ENTER].
Code:
#include<process.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#include<conio.h>
int usbox,i,j,nandu=0,result,cusb,cobox,r,d;
int playl=0;
char copybox[10];
[Code] ....
View 2 Replies
View Related
Apr 13, 2013
how can I make a class non inheritable,,, without using keyword "final" ?
class A
{
//
};
A obj; // No error
class B : public A
{
//
}; // error
View 2 Replies
View Related
Jun 24, 2014
Is the bool keyword in C exactly like the boolean keyword in Java? why this code is getting an 'unknown type error' when I initiialze looping.
#include <stdio.h>
#include <stdlib.h>
int main()
[Code].....
If I am completely using the boolean concept wrong in C, should I just use break statements instead?
View 11 Replies
View Related
Oct 11, 2014
In C++ by FLTK, to define a circle we use some code like this:
Circle c(Point(x,y), r);
And we can using vector_ref put and save them in a vector, like:
Vector_ref<Circle> vc;
vc.push_back(new Circle(Point(x,y),r));
Ok, those were about Circle and no problem till now!
Triangle can be defined like this for using in codes:
Graph_lib::polyline poly;
poly.add(Point(x1,y1),r1);
poly.add(Point(x2,y2),r2);
poly.add(Point(x3,y3),r3);
and this is a vector to saving them:
Vector_ref<Graph_lib::Polygon> vp;
The problem is that how to save/put triangles/polylines into that vp vector using new keyword like the circle does?
My sig: Save Cobani.
View 4 Replies
View Related
Jan 30, 2014
I think there is no always need of keyword extern in order to declare variables globally. Is it right?
For example I can declare a variable globally in one file and use it in some other provided that I have included the last one file ( that has the declaration of the variable of course) and compile these files together :
gcc -c f1.c f2.c for example
View 6 Replies
View Related
Nov 8, 2014
I'm working on a program that reads in a .txt file and searches through the text for a keyword. If it gets a hit on the keyword, the line number where the keyword is located and the line that contains the keyword is printed out. What I have now doesn't catch every occurance of the keyword "a".
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
[Code]......
View 4 Replies
View Related
Apr 25, 2014
I am reading a file whick looks like following:
Code:
10 = NAND(1, 3)
11 = NAND(3, 6, 5)
15 = NAND(9, 6, 2, 8)
Problem: I have to find the word "NAND" and then find the numbers inside the brackets because they are the inputs to that NAND gate. I have written a code below but that code can detect the fixed number of inputs. I need a code which can detect any number of inputs (whether 2 inputs or more than two). But i don't understand how do i do that?
My code:
Code:
string input_str ("INPUT"), output_str ("OUTPUT"), nand_str("NAND");
while (getline( input_file, line ))
{
std::size_t guard_found = line.find(guard_str);
[Code].....
View 8 Replies
View Related
Sep 5, 2013
i have read that inheritance slows down the process.because a_class -> b_class -> c_class it goes thorough many classes to find a function or a value. people advices not to do it more than a few times. what if instead of inheritance we are using headers, would it slow down the process also??i mean which one is faster? and arent both the same when we use headers or used inheritance.
View 3 Replies
View Related
Jan 17, 2014
I want to define a base class A which allows different type of logging (file stream, console, etc). In the meantime, I want a class B to to be able to call A's print function (i.e. derived class C's print function which defines the logging method). However, I don't want to make A's print function to be public because it will only be called by B.
class A {
protected:
virtual void print(void);
};
[Code] .....
View 1 Replies
View Related
Feb 18, 2015
I have 2 classes with a Function with the same definition (both inherited from the same base class) and in my derived class I derive from both of those 2. Is it possible to use the Methods of both classes? for example with an other name?
class A {
protected:
int print(int a) { std::cout << "int A: " << a << std::endl; }
};
class B : A
[Code] ....
is there something like using C::print as printc;?
The Problem, I have a Sprite class that derives from a Rectangle with properties Position, Size, Origin and Angle and a Text class that derives from Rectangle. Now i have a Button class deriving from both Sprite and Text.
- The Position, when moving the Button i have to change the position of both so i Implemented a new Method which calls SetPosition from the Sprite and the Text.
- The SetSize just affects the Button so i just did using Sprite::SetSize;
- The angle affects both so i just implemented a new Method and hide the other two
The problem is here:
- The Origin: writing button.SetOrigin(-1,0) should set the Origin of the Button and writing button.SetTextOrigin should set the Origin of the text.
Should i just reimplement a Mehtod named SetTextOrigin and call Text::SetOrigin from there and hide the button.Text::SetOrigin or is there something like using Text::SetOrigin as SetTextOrigin?
View 6 Replies
View Related
May 16, 2013
#include <iostream>
using std::cout;
using std::endl;
class CBox // Base class definition
{
public:
// Base class constructor
explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv)
[Code] .....
This example produces the following output:
// Derived class copy constructor
CCandyBox(const CCandyBox& initCB): CBox(initCB) {
std::cout << std::endl << "CCandyBox copy constructor called";
// Get new memory
m_Contents = new char[ strlen(initCB.m_Contents) + 1 ];
// Copy string
strcpy_s(m_Contents, strlen(initCB.m_Contents) + 1, initCB.m_Contents);
}
It will work right? Cause when I do "CBox(initCB)" it only sends the BASE part of the derived object to the base class copy constructor, is it copy or default?
View 6 Replies
View Related
Dec 20, 2013
I have Class A as a base class , and Class B , C derived classes from A and there's class D who have a data member (pointer to Array) of type A (Composition)
class D{
A **a;
int size;
.......
a = new A*[size];
......
};
And i have Print method , in its body i have to specific element (if it from class B or C ) with a given ID(both B and C have a data member ID ) there should be 2 options in print function .. printing elements for class B , or printing elements for class C ? how can i specific the elements ?
View 5 Replies
View Related
Oct 20, 2014
I have been reading up about object oriented programming recently and have come across 'Encapsulation, Polymorphism and Inheritance' as i understand it so far all OOP programs should use these three concepts. So i started thinking how do i get these concepts into my program as i am using WPF C# and i could not really find much good info about how these concepts apply to WPF programs.
Or do these concepts just not work with WPF programs?
View 5 Replies
View Related
Feb 23, 2014
I have two Form Employee and Instructor, here Employee is super class of instructor.
public class Instructor: Employee
{
//Instructor class
}
I am able to access all property of Employee inside instructor class but with these some textbox and button design inside Employee design is also getting inherited inside Instructor design and i don want this and i want to maintain the parent-child relationship between Employee and Instructor.
View 11 Replies
View Related
May 27, 2013
I'm writing a bunch of classes like Form, Button, etc. All these are derived from a base Object class. I want to make these objects auto-delete, so I only have to declare the new keyword, and the program will clean them when necessary. I understand the implications of dereferencing, I just have a couple of questions about my code.
I through this run-down example together:
Code:
#include <Windows.h>
#include <map>
class Object {
public:
Object() {};
~Object() {};
void *operator new(size_t size);
void operator delete(void *ptr);
[Code] .....
I have added a static variable of std::map type called m_Dynamic. I have overloaded the new and delete keywords (within the Object class) to keep the m_Dynamic map up-to-date. This prevents objects created on the stack from being deleted.
The object itself can then be deleted via the base Dispose() method. Or methods such as Destroy() can be added in derived classes and call upon Object::Dispose() themselves. This can be overloaded, etc, but eventually will be removed from the public view. I only have it here for testing.
From what I can tell everything "works", though I'm uncertain that the object is being correctly deleted.
Though, my main concern is that when a derived class such as Circle calls Dispose() which in turns fires delete this. Does it free() the sizeof(Object) or does it correctly free() the sizeof(Circle)?
Are there any other things I should be vary of? I only just started playing with new/delete overloading yesterday.
View 14 Replies
View Related
Jun 6, 2012
Shape base class, line and Point derived classes. What should I declare in .h files and implement in .cpp files that this is array will be work.
My major concern refer to operator [] and assign (=) operator. As far as I understand I should overload ([]) and (=) three times for classes shape , line and point or not... or is it possible made through virtual function? How will be code looks like ?
Code:
// part of main.cpp
Shape* shapes[3]; // Array of pointers to Shape
shapes[0] = new Shape();
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
shapes[2] = new Point(11,44);
cout << "using ToString function" << endl;
for(int i=0; i < 3; i++)
cout << s[i]->ToString();
for(i=0; i < 3; i++)
delete s[i];
View 2 Replies
View Related
Aug 5, 2013
let me show you the (simple) code:
Code:
#ifndef DUCK_H_
#define DUCK_H_
#include<iostream>
using namespace std;
class Duck
[Code].....
When I compile, it says: "error: 'virtual void Duck::display()' is protected" how come I can't gain access to MallardDuck's display(), which is public?
View 3 Replies
View Related