C++: Assessing class members from main

This entry is part 52 of 61 in the series C++

We have always say that a public member of a class can by assessed by any functions.

A good way to look at it is in accessing it from main function.

In main, only public members can be accessed.  It would generate an error if you try to access a private or a protected member.

#include <iostream>

using namespace std;

class Music {
    
public:
    int x;
protected:
    int y;
private:
    int z;
    
};

int main() {
  
    Music music;
    
    music.x = 6;
    cout << music.x;
    
    // Main will not be able to access y & z.
    // music.y = 8;   //error
    // music.z = 10; //error
    
}

public

C++: Members of a subclass

This entry is part 48 of 61 in the series C++

Here we work out a simple example of a base class and a subclass shown in the example below.

#include <cstdlib>
#include <iostream>
 
using namespace std;
 
// Base class
 
class Music {
 
public:
    string singer;
    string title;
};
 
// Derived class
 
class Rock : public Music {

public:    
    string album;

};
 
int main(void) {
     
    Rock rock;
    
    rock.singer = "Katy Perry";
    cout << rock.singer;
 
    return 0;
}

Rock is a subclass (or derived class) of Music and therefore it inherits all Music members (base class) other than constructors.

An instance of Rock therefore has 3 data members: singer, title and album.

subclass

 

 

 

Java: Access to classes and members

This entry is part 45 of 54 in the series Learn Java

By default, top-level classes are accessible within the package in which they are defined. However, if a top-level class is declared public, it is accessible everywhere.

The members of a class are always accessible within the body of the class. By default, members are also accessible throughout the package in which the class is defined.

This default level of access is often called package access. It is only one of four possible levels of access. The other three levels are defined by the public, protected, and private modifiers.

package javaapplication40;

public class Music {

    private String artiste;
    protected String rockband;

    public void live() {
        this.artiste = "Lady Gaga";
        System.out.println(artiste);
    }

    public void record() {
        this.rockband = "Nirvana";
        System.err.println(rockband);
    }

    public static void main(String[] args) {

        Music m = new Music();
        m.live();
        m.record();

    }
}

access

PHP & OOP: Properties of a class

This entry is part 6 of 11 in the series PHP & OOP

Class member variables are called ‘properties‘.

When I first started learning OOP using C++, I thought this is odd.  It sounds abstract.

But look at the following declarations for the 2 class members and you might be able to understand.

<?php

class MovieStar
{
    public $person = 'Tom Cruise';
    public $salary = 10000;
    public $movie = 'Mission Impossible';
    public $no_of_movies = 20;
}

class PopStar
{
    public $person = 'Madonna';
    public $salary = 8000;
    public $album = 'Crazy For You';
    public $no_of_album = 12;
}

?>

From the class properties such as $movie and $album, we could guess what the 2 classes are about.