Private String

Private String



πŸ›‘ ALL INFORMATION CLICK HERE πŸ‘ˆπŸ»πŸ‘ˆπŸ»πŸ‘ˆπŸ»

































Private String



.NET










Languages







C#



F#



Visual Basic





Workloads







Web



Mobile



Cloud



Desktop







Windows Presentation Foundation



Windows Forms



Universal Windows apps





Machine Learning & Data







ML.NET



.NET for Apache Spark



Entity Framework







APIs







.NET Core



.NET Framework



ASP.NET



ML.NET





Resources







What is .NET?



.NET Architecture Guides



Learning Materials



Downloads



Community



Support



Blog





More







Languages







C#



F#



Visual Basic





Workloads







Web



Mobile



Cloud



Desktop







Windows Presentation Foundation



Windows Forms



Universal Windows apps





Machine Learning & Data







ML.NET



.NET for Apache Spark



Entity Framework







APIs







.NET Core



.NET Framework



ASP.NET



ML.NET





Resources







What is .NET?



.NET Architecture Guides



Learning Materials



Downloads



Community



Support



Blog















Download .NET









Yes



No


The private keyword is a member access modifier.
This page covers private access. The private keyword is also part of the private protected access modifier.
Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared, as in this example:
Nested types in the same body can also access those private members.
It is a compile-time error to reference a private member outside the class or the struct in which it is declared.
For a comparison of private with the other access modifiers, see Accessibility Levels and Access Modifiers .
In this example, the Employee class contains two private data members, name and salary . As private members, they cannot be accessed except by member methods. Public methods named GetName and Salary are added to allow controlled access to the private members. The name member is accessed by way of a public method, and the salary member is accessed by way of a public read-only property. (See Properties for more information.)
For more information, see Declared accessibility in the C# Language Specification . The language specification is the definitive source for C# syntax and usage.

Java private Keyword
private keyword - C# Reference | Microsoft Docs
private string setters and getters - C++ Forum
Private , Abstract, Array And String Constructor In Java
Home | Java By Examples
This website uses cookies. By continuing, you give permission to deploy cookies, as detailed in our privacy policy . ok


Forum
Beginners
private string setters and getters



Last edited on Mar 8, 2013 at 6:16pm




Never did .net before...Just got hooked on trying C++.

My research said that you only declare in the header and define in the class .cpp.

Are you saying that the constructor definition go in the header?


Last edited on Mar 8, 2013 at 6:42pm




Last edited on Mar 8, 2013 at 6:43pm




@Peter87

Well, that was embarrassing, and I know better then those 6 errors.

@mutexe

Thank you for the sample. I understand the process of objects. What I am
trying to figure out is how to get the name to change through the setters and getters.


Last edited on Mar 8, 2013 at 6:56pm




Last edited on Mar 8, 2013 at 7:22pm




Last edited on Mar 8, 2013 at 7:32pm




There's actually several false practices in this thread.

1. Getters and setters are bad. For one, you don't need getters - other code should not change its behavior just because the object it is dealing with has a different state; that goes against encapsulation. For two, you should not use setters - it means external code has to calculate the new state of the object, which is also against encapsulation.

Instead, prefer objects that explain themselves (e.g. they have their own function for drawing themself to the screen) and which allow you to change their state indirectly. You can attack an object, but you shouldn't perform calculations for it and set its new health.

Basically, your getters and setters are bad, but your "behaviors" are good.

2. Prefer to define functions inline when possible. If you define everything in the CPP file, that makes it much much harder for the compiler to optimize your code. If everything is available inline in the header with the class definition, then the compiler can easily optimize your code and make it run faster.

Obviously for writing things like libraries, you should use PIMPL, but for making the classes for your game you should try to inline as much as possible to take advantage of compiler optimizations.

The whole "always define functions in CPP file" can also backfire: many people who try to learn templates often end up making a templated class and defining the function in the source file, which is wrong - it will not work. Templates must be defined inline, or their definition must be visible wherever they are used.


You can feel free to dispute or disagree with my points, but they're from my personal experience and readings, and they're working very well for me. Obviously they don't apply 100% of the time, but they usually do.


Last edited on Mar 8, 2013 at 7:44pm




@ AbstractionAnon

I told myself to stop copying and pasting I don't know how but that could be the only reason for line 22. Thank you....

@ L B
????????

I thought getters and setters to protect the private data was supposed to be a good thing. That is the reason I have been making sure to do it. Isn't it standard?


Last edited on Mar 8, 2013 at 7:52pm




Last edited on Mar 8, 2013 at 8:05pm




I guess that the way I am doing it is more coding and typing...but I looked around the I-net and they say it is worth the extra work. I don't know....
I don't really see anything wrong with it.

Thank you everyone I am not sure I am not missing anything, but I am not stuck anymore which is awesome.


What's wrong with it is that it is pointless as it still goes against encapsulation and proper OO class design. I'm not expecting you to understand instantly, but you will see why later.


Thank you Anon!!

I actually searched around and found little information. Even made a thread on here.

You just summed it up as I thought it should be! (:

Topic archived. No new replies allowed.

Home page | Privacy policy Β© cplusplus.com, 2000-2020 - All rights reserved - v3.2 Spotted an error? contact us

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include
#include

using namespace std;

#ifndef CDOG_H
#define CDOG_H

class CDog
{
private :
string name();
int weight();
bool rabid();
public :
CDog();
~CDog();
// Attributes
void setRabid( bool x);
bool getRabid();
void setName(string x);
string getName();
void setWeight( int x);
int getWeight();

// Behaviours
void growl();
void eat();
};

#endif // CDOG_H
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

#include
#include
#include "CDog.h"

using namespace std;

CDog::CDog( int x, string x)
{
rabid = false ;
weight = x;
name = x;
}

CDog::~CDog()
{

}

void CDog::setRabid( bool myboolean)
{
name = myboolean;
}

bool CDog::getRabid();
{
return (rabid);
}

void CDog::setWeight( int y)
{
weight = y;
}

int CDog::getWeight();
{
return (weight);
}

void CDog::setName(string y)
{
name = y;
}

string CDog::getName();
{
return (name);
}
// Behaviours

void CDog::eat()
{
cout << name << "is now eating" <<
weight++ << endl;
}

void CDog::growl()
{
cout << "Grrr" << endl;
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include
#include
#include
#include "CDog.h"

using namespace std;

int main()
{
CDog* c1;
c1 = new CDog(14, "Bob" );
CDog c2(7, "Ethel" );

c2.eat();
c1->growl();

getch();
return 0;
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

// in your header
class Wibble
{
private :
int m_nAge;
public :
Wibble();
~Wibble();
int getAge();
void setAge( int age);
};


// in your .cpp
Wibble::Wibble(){}
Wibble::~Wibble(){}

int Wibble::getAge()
{
return m_nAge;
}

void Wibble::setAge( int age)
{
m_nAge = age;
}

// in your main.cpp
int main()
{
Wibble myWibble;

myWibble.setAge(42);

return 0;

}
string name();
int weight();
bool rabid();
plus you have not declared this constructor in your header file.
2. plus you have not defined this constructor in your header file.
Are you saying that the constructor definition go in the header?
My research said that you only declare in the header and define in the class .cpp.
Are you saying that the constructor definition go in the header?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// include the class.h
#include "sampleClass.h"

#include

int main()
{
sampleClass myClass;

// They already have default values,
// because of the class constructor.
std::cout << myClass.getText() << '\n' ;
std::cout << myClass.getInt() << '\n' ;

std::string myString = "Text added via a setter." ;
int myInt = 20;

// Use the setters to add data to the private variables.
myClass.setText( myString );
myClass.setInt( myInt );

// cout the new values.
std::cout << myClass.getText() << '\n' ;
std::cout << myClass.getInt() << '\n' ;

return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#pragma once

#include

class sampleClass
{
public :
sampleClass( std::string t = "Uninitialized" , int i = 0 );
~sampleClass( void );

// getters/setters
void setText( std::string &t );
std::string getText();

void setInt( int &i );
int getInt();

// Create some private variables.
private :
std::string text;
int integer;
};

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include "sampleClass.h"

sampleClass::sampleClass( std::string t, int i ) : text( t ), integer( i )
{
}

sampleClass::~sampleClass( void )
{
}

void sampleClass::setText( std::string &t )
{
text = t;
}

std::string sampleClass::getText()
{
return text;
}

void sampleClass::setInt( int &i )
{
integer = i;
}

int sampleClass::getInt()
{
return integer;
}


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

#include
#include //

using namespace std;

#ifndef CDOG_H
#define CDOG_H

private :
string name;
int weight;
bool rabid;
public :
CDog();
~CDog();
CDog( int , string);
// Attributes
void setRabid( bool one);
bool getRabid();
void setName(string a);
string getName();
void setWeight( int x);
int getWeight();

// Behaviours

void growl();
void eat();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

#include
#include
#include "CDog.h"

using namespace std;

CDog::CDog()
{

}

CDog::~CDog()
{

}

CDog::CDog( int x, string a)
{
rabid = false ;
weight = x;
name = a;
}

void CDog::setRabid( bool myboolean)
{
name = myboolean;
}

bool CDog::getRabid()
{
return (rabid);
}

void CDog::setWeight( int y)
{
weight = y;
}

int CDog::getWeight()
{
return (weight);
}

void CDog::setName(string b)
{
name = b;
}

string CDog::getName()
{
return (name);
}
// Behaviours

void CDog::eat()
{
cout << name << "is now eating" <<
weight++ << endl;
}

void CDog::growl()
{
cout << "Grrr" << endl;
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include
#include
#include
#include "CDog.h"

using namespace std;

int main()
{
CDog* c1;
c1 = new CDog(14, "Bob" );
CDog c2(7, "Ethel" );

c2.eat();
c1->growl();

getch();
return 0;
}

struct MyClass
{
int health, age;
};

//...

MyClass mc;
mc.health = 10;
mg.age = 1;
//...
mc.health = mc.health-attackstrength*damageratio+defense*multiplier;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class MyClass
{
int health, age;
public :
int getHealth(){ return health; }
int getAge(){ return age; }
void setHealth( int h){ health = h; }
void setAge( int a){ age = a; }
};

//...

MyClass mc;
mc.setHealth(10);
mg.setAge(1);
//...
mc.setHealth(mc.getHealth()-attackstrength*damageratio+defense*multiplier);
I don't really see anything wrong with it.

White Water Rafting Is An Outdoor Foreign
Outdoor Wood
Reverse Missionary
Xhamster Photo Bbw
Pads Xhamster

Report Page