public

Can there be two public section in a class? If yes then why ? And in which circumstances we do so?

一曲冷凌霜 提交于 2019-12-18 11:28:26
问题 There is something bugging me about classes. For example class A { public: A() { ..... ..... } void cleanup() { .... .... .... } public: UINT a; ULONG b; }; In the above example there are two public section. In the first section I am defining a constructor and a method and in the second section I am declaring data members. Is the above class i.e. A correct. Can we do that? If yes then why is that needed and in what circumstances should we use it? Since we can do the entire thing in one

Can there be two public section in a class? If yes then why ? And in which circumstances we do so?

情到浓时终转凉″ 提交于 2019-12-18 11:28:10
问题 There is something bugging me about classes. For example class A { public: A() { ..... ..... } void cleanup() { .... .... .... } public: UINT a; ULONG b; }; In the above example there are two public section. In the first section I am defining a constructor and a method and in the second section I am declaring data members. Is the above class i.e. A correct. Can we do that? If yes then why is that needed and in what circumstances should we use it? Since we can do the entire thing in one

'public static final' or 'private static final' with getter?

霸气de小男生 提交于 2019-12-18 10:02:52
问题 In Java, it's taught that variables should be kept private to enable better encapsulation, but what about static constants? This: public static final int FOO = 5; Would be equivalent in result to this: private static final int FOO = 5; ... public static getFoo() { return FOO; } But which is better practice? 回答1: There's one reason to not use a constant directly in your code. Assume FOO may change later on (but still stay constant), say to public static final int FOO = 10; . Shouldn't break

Why does protected inheritance cause dynamic_cast to fail?

守給你的承諾、 提交于 2019-12-18 03:46:08
问题 I changed my C++ base class to be protected inheritance and my dynamic_cast (s) stopped working. Why should changing the inheritance to protected change the behavior of dynamic_cast ? struct Base { static Base *lookupDerived(); // Actually returns a Derived * object. }; struct Derived : protected /* Switch this to public to get it working */ Base { static void test() { Base *base = lookupDerived(); if (dynamic_cast<Derived *>(base)) { std::cout << "It worked (we must be using public

How to write a simple class in C++?

天大地大妈咪最大 提交于 2019-12-17 22:35:52
问题 I have been reading a lot of tutorials on C++ class but they miss something that other tutorials include. Can someone please show me how to write and use a very simple C++ class that uses visibility, methods and a simple constructor and destructor? 回答1: Well documented example taken and explained better from Constructors and Destructors in C++: #include <iostream> // for cout and cin class Cat // begin declaration of the class { public: // begin public section Cat(int initialAge); //

Github (SSH) via public WIFI, port 22 blocked

喜你入骨 提交于 2019-12-17 15:02:39
问题 I'm currently on a public WIFI spot and I'm unable to use SSH (they probably blocked that port). However, I need that connection to do a git push . ➜ ssh -T git@github.com ssh: connect to host github.com port 22: Connection refused Is it possible to bypass this restriction by setting up a SSH tunnel via port 80 and tell github push to use that connection? How to do that? I'm on OSX (lion). This must be a common problem? 回答1: Try this: $ vim ~/.ssh/config Add Host github.com Hostname ssh

Public static variable value

时光怂恿深爱的人放手 提交于 2019-12-17 14:01:35
问题 I'm trying to declare a public static variable that is a array of arrays: class Foo{ public static $contexts = array( 'a' => array( 'aa' => something('aa'), 'bb' => something('bb'), ), 'b' => array( 'aa' => something('aa'), 'bb' => something('bb'), ), ); // methods here } function something($s){ return ... } But I get a error: Parse error: parse error, expecting `')'' in ... 回答1: You can't use expressions when declaring class properties. I.e. you can't call something() here, you can only use

Difference between public static and private static variables

三世轮回 提交于 2019-12-17 10:29:26
问题 class Employee{ // salary variable is a private static variable private static double salary; // DEPARTMENT is a constant public static final String DEPARTMENT = "Development"; public static void main(String args[]){ salary = 1000; System.out.println(DEPARTMENT+ " average salary:"+salary); } } This java program contains a static variable. But I cannot understand the difference between public and private static variables. 回答1: A public variable is accessible everywhere in the code - a private

Changing public data to private data in Java

岁酱吖の 提交于 2019-12-14 03:13:27
问题 I want to change the default constructor (person1) from public to private and then ask the user to input the information instead of the given values. I have two different classes - Person; which is encapsulated and then PersonTest which tests the information. class Person { // Data Members private String name; // The name of this person private int age; // The age of this person private char gender; // The gender of this person // Default constructor public Person() { name = "Not Given"; age

Why Not Use Public Member Functions

我是研究僧i 提交于 2019-12-14 03:06:01
问题 Even if I make class member functions public and it is consumed by other client applications, implementation details of functions will never get exposed to client. Why make member functions protected or private? For example, if my class is Math, with public function sum(int, int b) then only interface/declaration part will be exposed to client and not implementation. public class Math { public int sum(int, int b) { //Implementation } } public class Client { Math objMath = new Math(); objMath