3. Objects #

Created Wednesday 08 January 2020

Syntax #

class_name object_name; // this allocates memory. variable of type student in the stack.
class_name * object_name = new class_name; 	// this creates a pointer in stack to a memory location in heap.

Q) Can we create an object anywhere we like? A) We can create anywhere after the class has been defined.

#include "absolute_path/class_file_name.extnsn" // different directory
#include "file.extnsn" // file in the current directory

Accessing members of an object #

obj.name; // access 'name' attribute of 'obj' object
obj.fact() // call fact() function of 'obj'
// . is called the selector operator
(*p).name;
(*p).fact();

// C++  has a better way, a simple expression. Does exactly the same thing.
p->name;
p->fact();

Concept of Access Specifier in C++ #

Syntax:

class A{
	public:
		int a;
		int b;
	private:
		void f();
};
class A{
	public:
		int a;
		int b;
	private:
		void f();
	public: //allowed
		int c;
};

Notes:

class Car {
  int a; // a is private
};