C++ :: Make A Class Without Any Data Member?
Aug 18, 2013can we make a class without any data member ? but it may have member functions ! in c++
View 2 Repliescan we make a class without any data member ? but it may have member functions ! in c++
View 2 Repliesis this possible to make a class without any data member in c++ ?
View 1 Replies View RelatedThe question says that:
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
[Code].....
But then the 2nd part of the question itself says that "Acno entered by the user". So we need to identify/search for an account with that Account Number. How can we do this without comparing Acno with S.Acno? I know that S.Acno is not accesible as it is a private member but then how to compare Acno without even using a member function(public) that can return the value of Acno?
class Date
Date(int=1, int=1, int=1990);
class Person
Person(string="", string="", Date=NULL);
class RealEstateAgent:Public Person
RealEstateAgent(string="",string="",Date=NULL,Date=NULL,int=NULL, double=0.0);
}
[code]....
how can I assign default values with Customer object and RealEstateAgent?
I have some doubt regarding class data member accessing in another file.Follwing code showing error.
class A://file a.cpp
{
public:
int add;
int sub;
};
//file b.cpp
extern class A
void cal()
{
A::add=A::sub;
}
If I want a class with a vector data member, can I specify it as follows?
std::vector< bool > integers( 101 )
I'm having some problems when compiling code.
I recently discovered the new - new to me anyway! - feature of modern C++ that allows you to set the initial value of a data member when you declare it:
class CINTWrapper{
private:
int m_iData=0;
};
This even extends to calling member functions that work with initialization I believe:
class CStringWrapper{
private:
wchar_t* Allocate_Array(const int iBufferSize);
wchar_t* m_pString=Allocate_Array(1);
};
At first, this seemed an extremely useful piece of functionality that C++ had been lacking all along. However, the more I thought about it the more it struck me this feature actually undermines one of the principle design elements of the language - that being the Constructor.
As I understand it the primary purpose of the Constructor is specifically to give the programmer a place where it is guaranteed he can always initialize his data members before anything else is done with the class. However, given the new initialization rules this is no longer necessary. So it largely seems to me that Constructors as a whole are no longer necessary either! Copy-Constructors are a special and vital case. Admittedly when I was using them for their intended purpose I hated either the redundancy you had to introduce across multiple Constructors; those with and without arguments and so on, or alternately the fine tuning of helper-functions to do common initialization between these variants. Now however I sort of regret this cast-iron rule has been taken away.
As a last point, I am trying to change the way I think about programming. I am trying to employ more objects than pure C-style ('int' or 'double', etc) data types and especially to move into templates (although absolutely NOT the Hewlett Packard template library!). Given my current understanding of inheritance in particular it seems to me that using pre-initialized data members rather than Constructor-initialization makes object derivation even more complicated, not less so.
What is the size of object in c++ , if there is no data member in the class ?
View 3 Replies View RelatedLet's take a look at the code first,
Code:
class B
{
public:
int data;
};
class D : public B
{
public:
int data;
};
Both B and D defines data. I wonder if there is any difference between them?
I have a small class with a static int data member. I want to zero init it. I am thinking that making a .cpp file with only one line seems too much, isn't it?
So, can I do it inside the the header file? The variable is going to enumerate how objects were created (so any alternative will do).
"You cannot initialize the static data member in the class definition — that’s simply a blueprint for an object and initializing values for members are not allowed. You don’t want to initialize it in a constructor, because you want to increment it every time the constructor is called so the count of the number of objects created is accumulated."
Why don't you want to initialize it in a constructor?
Edit: Because every time it is called it will set it back to 0 or whatever the initializing value.
I have to implemente the to_string method. Whats the fastest way? Stringstreams. But I have to use C++ without any headers, so I need to implement the stringstream class. How can an stringstream hold one float? An double? Hoq cqn I implement an strigstream myself?
View 8 Replies View RelatedFor example, in a header file A.h, I define an abstract class,
Code:
// A.h
class A {
public:
virtual void foo() = 0;
private:
static int _x;
};
How'd I initialize static member data _x?Normally, we initialize a static member data in a cpp file. However, there is not cpp file for A.h. If I intialize _x in header file, there will be linker errors like mulitple defined symbols. What is appropriate way to do that?
Basically I want to create a base class which defines a static data member so that its automatically redeclared as the same static data member in the derived class.
class A{
protected:
static derivable int val;
// A::val
}
class B : public A{
// static derivable int val is already here
// A::val AND B::val
}
This seems impossible to me but I'm wondering if perhaps there's a way to add modifiers to the compiler to do this (or preferably something MUCH simpler)...
I am trying to make a table class that will be able to have multiple columns of data. I want it to have something to hold data (I was using a 2D vector for only one data type) in it, somewhat like a pair, but for any number of data types. The class is a template to make it generalized.
I have looked a little at variadic templates, but I don't know how to declare the vectors for each data types then.
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] ,....
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] ....
So I have an ImageManager class, Board class, and Box class. In Board.h I can declare ImageManager imgr; and in Board's constructor I can use imgr and its functions and such. However, in Box.h when I try and declare ImageManager imgr; I get the error "cannot access member declared in class ImageManager". Both declarations are under private, and exactly the same, but one doesn't work. Also, is there a way to only have one instance of ImageManager?
View 19 Replies View Relatedit seems everytime i use statics in a class i come across with a porblem.
this time i wanted to make a class i created static inside another class.
MainVariables.h file
static fge::window mWinMain;
if someone ever wants to reach it
MainVariables.cpp file
fge::window MainVariables::mWinMain;
...
...
fge::window MainVariables::GetWinMain()
{
return mWinMain;
}
but when i created some other MainVariables classes at other places instead of them reaching to the same window two window is being created.
yes i know maybe there are better methods but currently i m working on polymorphism and i need some static members.
I was looking at some linked list material and was wondering something. Can you initialize a data member inside a struct like in C++? i.e.
Code:
typedef struct node
{
int data;
struct node * next = NULL; // this is the line in question
} LLnode;
I am getting this error invalid use of non static data member.my code looks something like this: i have a main.cpp and 2 class files with respective .h files, say one class file is named human (so i have human.cpp and human.h) and stats (so i have stats.cpp and stats.h) in my stats.h file, i have a double array: double HumanF[10][12] with everything filled in.then in my human.h file i just have a bunch of integers. human.cpp has formulas in it that use numbers from the double array i mentioned. for example
Human::Human() {
constant (this is a double i made in human.h) = (1+Stats::HumanF[0][0]);
i (another double) = pow(constant, ylvl);
(ylvl is also an int I made in my header file)
yhp = i*137;
}
Here's a part of my program. What I need to know is how I can pass an argument to the Book constructor so I can change the const data member Category (with cascading capacity if possible. I also posted some of my set functions for further comprehension.
class Book {
friend void CompPrice(Book &,Book&);
//friend function that has access to the member functions of this class
//The arguments sent to it are by address, and of type the class Book, so that it can have access to its member functions
private:
//private data members
[Code]...
What I need to know is how I can pass an argument to the Book constructor so I can change the const data member Category (with cascading capacity if possible. I also posted some of my set functions for further comprehension.
class Book
{
friend void CompPrice(Book &,Book&); //friend function that has access to the member functions of this class
//The arguments sent to it are by address, and of type the class Book, so that it can have access to its member functions
private: //private data members
[Code].....
Programe #1
// file.h
class File {
public:
static const int var = 9;
};
[Code]....
Program#1 is running fine, but program#2 gives linker error:
error LNK2005: "int GlobalVar" (?x@@3HA) already defined in file.obj
I know the header files are never compiled. Then in the above case, how the compiler knows the definition of variable var, but not able to find the definition of GlobalVar? What is the difference between this two programs?
I am having compiling issues and am looking for an explanation as to what is causing the error and how to fix it. The declaration of 'g4vuplInstanceID' seems to be global in scope in my option, however I may be wrong.
Compiler Error: error: 'class myPhysListGeneral' has no member named 'g4vupInstanceID'
#include "G4VUserPhysicsList.hh"
#include "G4VUPLSplitter.hh"
#include "G4VPhysicsConstructor.hh"
#include "myPhysListGeneral.hh"
#include "G4ParticleDefinition.hh"
#include "G4ProcessManager.hh"
#include "G4ParticleTable.hh"
[code]....
How can a member function in my derived class call the same function from its base class?
View 1 Replies View Related