Local and Nested Classes in C++

In a C++ language we can define a class within a function and such class are called local class.

Inside the local class we can access global and static variable.

Local class can’t access the normal/non static variable of function.

Output

Description: In the above program, class B is defined within main() function means class B is local class.

Local class B can access the static variable “z” of main() function as well as can access  global variable “w”.

Local class can’t access the non static variable of function

Output

Nested  Classes

In the C++ programming, we can declare the class within another class and a  class declared as a member of another class is called as a nested class.

 Syntax of  nested class declaration:

Que. Write a C++ program to create a class “student”  which contains the data member such as name, roll number  and also consist one nested class “date” ,whose data are day, month and year. The value of student read from keyboard and display using nested class.

Output

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Lowercase character to uppercase character
  4. Command Line Argument
  5. Local vs Global Object in C++
  6. Arithmetic operations using switch case
  7. Switch Case in C Programming
  8. Constructor in C++
  9. Know more about C++
Categories C++

Friend Function in C++

In a C++ programming language private member of the class can accessed by member function.

Any non-member function can’t access private data member of the class.

But if we want to access the private data in non member function it is still possible by using the “friend function”.

A friend function can access private data of the class.

This can be done by declaring a non member function as a friend of the class whose private data is to accessed .

We can declare a non member function to friend function by using  “friend”   keyword.                                           

The function can be declared as friend function of one or more class.                                                                         

Properties of friend function

For the friend function there is no scope restriction means it can be called directly without using object.

friend function can’t access the public/private member directly.

It use object and dot operator to access the public/private member variable of the class.

Friendship is not shared for ex: if class X is declared friend of class Y, this dose not mean that Y has privileges to access private member of class X.

Output

Description : In the above program,  show() is a non member function( it defined outside of class and called from main() function ) which is declared as a friend function of class “A”. 

Que: Write a C program  to exchange the values between two class by using friend function.

Output

Categories C++

Array of Object in C++

As we know array is a collection of similar data elements. We can also create array of object.

Que. WAP to create a array of object. initialize and display the content of array.

  OR  WAP  to create a class player and take a details from user name, age and print. Also use  array of object.

Output

Note: In the above program we have creates three object “player[3]”.

Categories C++

String reverse program in java

String Reverse using StringBuilder

Leap Year Program in Java

A Leap Year is a year which has 366 days. This year February is of 29 days. Leap years occurs in every four years.

A leap year has following constraints.

We can check in programming as below

  • A year is divisible by 400 then it is a leap year
  • A year is divisible by 4 and not divisible by 100 then the year is leap year

See another program