C++ Class Declaration
Introduction
A class is declared using the class keyword with members under access specifiers (public, private, protected).
Definition
class ClassName { access: members; };
C++ Code Example
class Rectangle {
private:
int w,h;
public:
void set(int a,int b){ w=a; h=b; }
int area(){ return w*h; }
};Important Questions
Short Questions
- Syntax of class declaration?
- Default access in class?
- What is access specifier?
- Can class have constructor?
Long Questions
- Write a class declaration example.
- Explain access specifiers in class.
MCQ Practice
1. Default access in class is:
- private
- public
- protected
- none
Answer: private.
2. class keyword is used to:
- Declare class
- Create object
- Define function
- Allocate memory
Answer: Declare class.
3. Access specifiers include:
- public, private, protected
- static, const
- int, float
- new, delete
Answer: public, private, protected.
4. Members under private are:
- Hidden
- Public
- Inherited always
- None
Answer: Hidden.
5. A class can contain:
- Data and functions
- Only data
- Only functions
- Nothing
Answer: Data and functions.
Quick Revision
- Default access is private.
- Use public/private/protected.