How to access a private structure within a class in C++? - c++

Good day everyone! I would just like to ask if how it is possible to access a private structure inside a class?
My code looks like this:
class VideoClass{
private:
struct vidstruct {
int Video_ID;
string movietitle;
string genre;
string prod;
int numberOfCopies;
string MovImg_name;
};
public:
VideoClass();
// ~VideoClass();
void insertVideo(vidstruct info);
void rentVideo(int rv); //
void returnVideo(); // ---
void showDetails(int sd);
void validateVideo(); //
void displayVideo();
};
What I wanted to is to access the vidstruct (name of the structure) to any parts of my program. Thankyou for your kind answers in advance :)

The fact that your member vidstruct is declared as private means that it is not accessible outside of the class. If it is meant to be used outside of the class You could declare it simply outside of it.
Here would be a snippet example (printing 'movie test'):
#include <iostream>
using namespace std;
typedef struct {
int Video_ID;
string movietitle;
string genre;
string prod;
int numberOfCopies;
string MovImg_name;
} vidstruct;
class VideoClass{
public:
VideoClass() {
};
// ~VideoClass();
void insertVideo(vidstruct info) {
cout << info.movietitle;
};
void rentVideo(int rv); //
void returnVideo(); // ---
void showDetails(int sd);
void validateVideo(); //
void displayVideo();
};
int main()
{
vidstruct x;
x.movietitle = "movie test";
VideoClass videoTest;
videoTest.insertVideo(x);
return 0;
}

Related

how to define the methods of a nested class outside the class definition [duplicate]

This question already has an answer here:
Nested Class Definition in source file
(1 answer)
Closed 28 days ago.
the code asks to define all member funtions outside the nested classes where the comment is written , and i couldnt figure it out ..
this is the code
#include<iostream>
#include<string>
using namespace std;
class Person
{
private:
string name;
int Age;
char Gender;
string Job;
public:
class Address
{
private:
string country_Name;
string Street_Name;
int Unit_No;
public:
Address();
void Set_Data(Person::Address &Obj);
static void Display_Address(Person::Address Obj1);
};
Person();
string Get_Name();
static int Get_Age(Person p);
char Get_Gender();
static string Get_Job(Person p);
Person& Set_Data_Person(Person::Address &Obj);
static void Display_Person(Person p, Person::Address addr);
};
// WRITE HERE -- MEMBER FUNCTIONS DEFINITIONS
int main()
{
Person P1;
Person::Address add;
P1= P1.Set_Data_Person(add);
cout << "-------------------------- Display Info----------" << endl;
P1.Display_Person(P1, add );
return 0;
}
i tried defining the setters for address and person class and i dont know where to begin
You can call the resolution operator twice to access the inner-class methods:
#include <iostream>
class A {
public:
class B {
public:
void hello();
};
void hello();
};
void A::hello() { std::cout << "Hello from A!\n"; }
void A::B::hello() { std::cout << "Hello from B!\n"; }
int main() {
A a;
A::B b;
a.hello(); // out: Hello from A!
b.hello(); // out: Hello from B!
return 0;
}

error:function definition does not declare parameters in setter

I wrote a simple c++ program and it gave me error in the setter, function definition does not declare parameters, although there was a setter above it and it worked correctly the error is in the function named setattack here is the code:
{
#include <iostream>
using namespace std;
class Warrior{
private:
string name;
int attack;
int defense;
public:
void setname(string m){
name=m;
}
string getname(){
return name;}
void setattack{int aw}{
attack=aw;
}
int getattack(){
return attack;}
void setdefense{int dw}{
defenset=dw;
}
int getdefense(){
return defense;}
void kill(const Monster &monster){
}
};
int main()
{
return 0;
}
}
In your set function you have used {} curly braces for arguments I think it should be (), and there is an extra } after main.

Stl list of struct as a class member

For my homework I have to do a class Game in C++ that has name, size, and a list of updates that contain date of update and some information about that update. (for example : 22.05.2018 Bug fixed at quest 3). Here is what i tried, but it doesn't work. Game.h:
class Game{
public:
struct update{
string date;
string info;
};
string name;
double size;
list<update>l;
Game(string name, double size, list<update>l);
virtual ~Game();
};
and in Game.cpp:
Game::Game(string name, double size, list<update>l){
this->name=name;
this->size=size;
this->l=l;
}
In int main I created a list:
int main()
{
list<update>mylist;
update u1,u2,u3;
u1.date="20.05.2018";
u1.info="Mission 3 bug fixed";
u2.date="25.05.2018";
u2.info="New quest";
mylist.push_back(u1);
mylist.push_back(u2);
Game g("Gta5",60.0,mylist);
return 0;
}
i get this error:
no matching function for call to 'Game::Game(const char [4], double, std::__cxx11::list<update>&)'|
Or if you want to keep the nested class update:
#include <string>
#include <list>
using std::string;
using std::list;
class Game{
public:
struct update{
string date;
string info;
};
string name;
double size;
list<update>l;
Game(string name, double size, list<update>l);
virtual ~Game() {}
};
Game::Game(string name, double size, list<update>l){
this->name=name;
this->size=size;
this->l=l;
}
int main()
{
list<Game::update> mylist; // use Game::update to access nested class
Game::update u1,u2,u3;
u1.date="20.05.2018";
u1.info="Mission 3 bug fixed";
u2.date="25.05.2018";
u2.info="New quest";
mylist.push_back(u1);
mylist.push_back(u2);
Game g("Gta5",60.0,mylist);
return 0;
}
Try this,
#include<bits/stdc++.h>
using namespace std;
struct update
{
string date;
string info;
update(string date,string info){
this->date=date;
this->info=info;
}
};
class Game{
public:
string name;
double size;
list<update>l;
Game(string name, double size, list<update>l);
// virtual ~Game();
};
Game::Game(string name, double size, list<update>l){
this->name=name;
this->size=size;
this->l=l;
}
int main(){
list<update> l,m;
l.push_front(update("10/10/2017","some bug fixed"));
double size=100;
string name="Game1";
Game obj(name,size,l);
cout<<obj.name<<" "<<obj.size<<" "<<endl;
m=obj.l;
list<update>::iterator i;
for(i=m.begin();i!=m.end();i++){
update structObj=*i;
cout<<structObj.date<<" "<<structObj.info<<endl;
}
return 0;
}
Output
Game1 100
10/10/2017 some bug fixed

How do I define a datatype to be an int in a structure without redefining my class type in a class object?

I am having difficulty resolving a redefinition error. Basically, I have a class object called houseClassType in my class header file and I also have to use houseClassType as my datatype for an array within my structure in my struct header file. Below are the two header files:
house header file:
#include "Standards.h"
#ifndef house_h
#define house_h
//Definition of class, house
class houseClassType
{
//Data declaration section
private:
int capacityOfGarage;
int yearBuilt;
int listingNumber;
double price;
double taxes;
double roomCounts[3];
string location;
string style;
//Private method to set the county name
string SetCountyName(string);
string SetSchoolDistrictName(string);
//Private method to set school district name
void SetSchoolDistrictName(void);
//Set function for the object
void ExtractLocationData(string& state, string& county, string& city,
string& schoolDistrictName, string& address);
//Methods declaration
public:
///Default Constructor
houseClassType(void);
///Get methods for data members - INLINE
int GetCapacity(void) { return capacityOfGarage; };
int GetYearBuilt(void) { return yearBuilt; };
int GetListingNumber(void) { return listingNumber; };
double GetPrice(void) { return price; };
double GetTaxes(void) { return taxes; };
string GetLocation(void) { return location; };
string GetStyle(void) { return style; };
void GetRoomCounts(double[]);
//Set methods for data members
void SetCapacityOfGarage(int);
void SetYearBuilt(int);
void SetListingNumber(int);
void SetPrice(double);
void SetTaxes(double);
void SetLocation(string);
void SetStyle(string);
void SetRoomCounts(double[]);
//Output methods for data members
void OutputLocationData(ofstream&);
void OutputStyle(ofstream&);
void OutputRoomCounts(ofstream&);
void OutputCapacityOfGarage(ofstream&);
void OutputYearBuilt(ofstream&);
void OutputPrice(ofstream&);
void OutputTaxes(ofstream&);
void OutputListingNumber(ofstream&);
void OutputHouse(ofstream&);
///Destructor
~houseClassType(void);
};
#endif
Realtor header file:
#include "Standards.h"
#ifndef Realtor_h
#define Realtor_h
const int NUMBER_OF_HOMES = 30;
typedef int houseClassType;
struct realtorStructType
{
string agentName;
houseClassType homes[NUMBER_OF_HOMES]; ///Redefinition error here
int numberOfHomes;
};
void InputHomes(ifstream& fin, string agentName, int& numberOfHomes);
#endif
Any help would be much appreciated.
The C++ language likes to have unique type names throughout a translation module. The following are not unique type names:
class houseClassType
typedef int houseClassType;
If you must use the same name, then you'll need to use namespaces to separate them:
namespace City
{
class houseClassType;
}
namespace Suburban
{
typedef int houseClassType;
}
struct realtorStructType
{
Suburban::houseClassType homes[MAX_HOMES];
};
I highly recommend you draw or design this issue first. This will help you with names too.
The simple solution is to use different names.
Also, do you need the suffix "ClassType" or "StructType" in your name? In a good design, whether it be a struct or class doesn't matter.
Your code is ambiguous. If you have
class houseClassType;
typedef int houseClassType;
What would the following code mean?
houseClassType x = new houseClassType();
You can resolve the ambiguity using a namespace, but it's better to change your second houseClassType type and name.
An example might look like this.
class House {
public:
enum class Type {
...
}
};

Calling correct virtual methods

Here is my code:
#include <iostream>
#include <string>
using namespace std;
class Sport{
protected:
string name;
double hours;
virtual double returnCalories()=0;
public:
Sport():name("Not defined"),hours(0.0){}
Sport(string n, double c):name(n),hours(c){}
virtual ~Sport(){}
void setName(string x){
name=x;
}
void setTime(double x){
hours=x;
}
};
class Running:public Sport{
public:
static const int CALORIES = 950;
Running(){}
~Running(){}
double returnCalories(){
return hours*CALORIES;
}
};
class Activity{
public:
Sport* one;
Activity(){}
Activity(string n,double time){
one->setName(n);
one->setTime(time);
}
~Activity(){}
};
class Diary{
private:
Activity list_activity[10];
int counter_activity;
public:
Diary():counter_activity(0){}
~Diary(){}
void addActivity(Activity x){
// add activities
}
double sumCalories(){
for(int i=0;i<10;i++){
if(list_activity[i].one->getName()=="Running"){
// I want to call returnCalories() of class Running
}
}
}
};
int main() {
Activity test("Running",3.2);
Diary test2;
test2.addActivity(test);
return 0;
}
Now I have a question:
How is it possible to call returnCalories() of class Running where I want to ? ( it's commented in the code )
Is this even possible, or should I change my logic somehow ?
It's crashing because you have not initialized Sport *one; and you're attempting to call methods on a null pointer. You need to first create a Running object within the Activity constructor using the "new" operator like so:
one = new Running(n, time);
Create an overloaded constructor in your "Running" class that takes the appropriate arguments as well, so that you can initialize your variable as shown above.