C Sharp :: What Is Mean By Instantiating A Class
Aug 17, 2014What is mean by instantiating a class.
View 8 RepliesWhat is mean by instantiating a class.
View 8 RepliesFor example I want to get the type of std::vector<>::size_type without specifying a template argument.
Here's what I want to do:
std::vector<>::size_type x{5};
Is there any way this can be done?
Whether it is possible to add a new class to a program? For example: program a is my main program and I want to use program b to just add a new class to program a.
View 5 Replies View RelatedIs there any way or trick to make a program that executes without using any class or class concept
View 4 Replies View RelatedHow to use static in a class, function and variable.
View 1 Replies View RelatedI have base class with some properties(variables).I want to inherit that class and want to change name of that properties in the derived class using concept of Shadowing.
View 1 Replies View Relatedcan we give the restriction to method for a particular class
View 1 Replies View RelatedAs i have one Abstract base class say MyBase.
It have parameterized constructor with string value it have abstract method call.And I also have One child class say MyChild.
It does not have any constructor only one have public method and i want to call that parameterized constructor of base class onto child class.
I need to create a GlobalConfig class. But I want to derive from it in another class.
Here's an example:
public class BaseConfig {
public string GlobalPath {get; set;}
}
public class ConfigA : BaseConfig {
public string pathA {get; set;}
}
public class ConfigB : ConfigA {
public string pathB {get; set;}
}
The idea behind is that I don't want to write the code multiple times and what's more important in class ConfigA I want to set GlobalPath and have access to it in ConfigB.
In other word I want class ConfigB to have a property GlobalPath which was set in class ConfigA.
To clarify I want to have only one object of Config in memory.
When I set BaseConf.GlobalPath to 'A', I want to access it from ConfigB.GlobalPath and also get 'A'.
I always design GlobalConfig as a static class, but static classes can't be inherited. So I tried to implement Singleton Pattern, but ConfigA can't find constructor of class BaseConfig because it's private.
In my WPF application I created a LINQ to SQL entities class. Then I created a partial class and there add code to my entity class. Below is the code of it:
namespace Mynamespace {
partial class rts_index {
#region Fields
/// <summary>
/// Status:
/// "Selected" - true, "Unselected" - false.
/// </summary>
private bool _is_selected;
[code]....
Then I executed LINQ to SQL. Works perfectly well. Then I bound WPF Datagrid radiobutton column to is_selected property of the partial class. Below is XAML code:
.
.
.
<Window.Resources>
<CollectionViewSource x:Key="rts_indexViewSource" d:DesignSource="{d:DesignInstance my:rts_index, CreateList=True}" />
</Window.Resources>
[code]....
And here I have the problem. When I'm changing the radiobutton status from Unchecked to Checked it doesn't change the value of is_selected property from false to true in the partial class. I don't know why.
I have a non form class. I want to update label/ check status of check box etc.. in non form class ( here resides functions that contains logic). How can i do that ?
View 4 Replies View RelatedThere are two ways to access the members of class A inside class B:
1) Making an instance of class A in class B
2) Deriving class B from class A
So what is the basic difference in both ways as we can do same kind of work with both ways?
The case is like
class B{
public:
somedata;
somefunction();
}
class A{
public:
data;
function();
}
in somefunction i want a pointer to current object of class A m new to c++
Say I have 3 classes:
class Player {
public:
virtual func1();
[code]....
Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:
//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);
When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
#include<iostream>
class A{
public:
[Code].....
I have my main.cpp like this:
#include <iostream>
#include "curve1.h"
#include "curve2.h"
using namespace std;
int main() {
Curve1 curve1Obj;
Curve2 curve2Obj;
[Code]...
Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.
My base class header score.h looks like this:
#ifndef SCORE_H
#define SCORE_H
class Score {
private:
int *ipScore;
float fAverage;
int iSize;
[Code]...
You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously).
.....
The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....
How can I cause it to return the old values that are set in curve1.cpp?
Here is my curve2.cpp
#include <iostream>
#include "curve2.h"
using namespace std;
void Curve2::curve() {
cout << "getSize() returns: " << getSize() << endl; // out comes the garbage
}
Can I use a function to simply put values from old to new variables? If yes then how?
How to initialize a static member of a class with template, which type is related to a nested class?
This code works (without nested class):
#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>
[Code] ,....
Linker error.
First off the error
Code:
Error1error LNK2019: unresolved external symbol "public: __thiscall ReachTop<class Character>::ReachTop<class Character>(class Character *)" (??0?$ReachTop@VCharacter@@@@QAE@PAVCharacter@@@Z) referenced in function "void __cdecl `dynamic initializer for 'gReachTop''(void)" (??__EgReachTop@@YAXXZ)Main.objDecisionTest
Reach Top class inherits from Goal Class
Goal Class
Code:
#ifndef _GOAL_H
#define _GOAL_H
#include "Action.h"
#include <list>
template <class T>
class Goal
[Code] ....
Code to create
Code:
Character* gCharacter = new Character(1, gWorld);
Goal<Character>* gReachTop = new ReachTop<Character>(gCharacter);
I can provide the character class and its inheritance aswell if you like.
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
Please consider the following code :
#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
Let's say I have a Car object , and it contains inner Engine object.
Code:
struct Car{
Engine mEngine;
};
In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:
Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}
Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?
Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};
The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
So I have a base class, lets call it base. In base I have a virtual function called update(), update just couts "base" then I have a class derived from base called derived;
it has a function called update(), update just couts "derived" then I create a vector called Vec it's initialised like this:
std::vector<base> Vec;
then I add an element into it like this
Derived DerElement;
Vec.push_back(DerElement);
then when I type:
for (int i=0; i<Vec.size(); i++) {
Vec.at(i).Update();
}
It outputs:
Derived DerElement2;
DerElement2.Update();
and it outputs this:
#include <iostream>
#include <vector>
class Base {
public:
virtual void Update() {
[Code] .....
and this is it's output:
Base
Derived
Press any key to continue . . .
I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:
class Derived;
class Base {
int i, j;
friend void Derived::f();
protected:
Base();
[Code] ......
I have an example where I have a variable belonging to a base class, but I would like to tell the compiler that it actually belongs to a derived class. How can I do this?
// base class: R0
// derived class: R1
// see function SetR1 for the problem
class R0 {
public:
int a;
[Code] .....
In the project I'm currently working on I define a class that only inherits from a parent class and takes one argument.
Does this class need to be defined in the header or source file? I read different answers around the internet.
Or is it better to always split definition and logica, even for something like an operator?
I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.
I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.
I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.
Lets have it as a sample code:
class Timer {
private:
int x;
public:
Timer();
get_X();
start_X();
[Code] ....