As you should expect this program displays the values 10 and 99 on the screen.
In this example, since a is declared as a public member of myclass, it is directly accesible from main. Notice How the dot operator is used to access a. In general, whether you are calling a member function or accessing a member variable, the objects name followed by the dot operator followed by the member's name is required to fully specify which object's member you are referring to.
This Program displays the following output:
#include<iostream.h>
class myclass{
//private to myclass
int a;
public:
void set_a(int num);
int get_a();
};
void myclass::set_a(int num)
{
a=num;
}
int myclass::get_a()
{
return a;
}
main()
{
myclass ob1, ob2;
ob1.set_a(10);
ob2.set_a(99);
cout<<ob1.get_a()<<"\n";
cout<<ob2.get_a()<<"\n";
return 0;
}
//This fragment contains an error.
#include <iostream.h>
main()
{
myclass ob1,ob2;
ob1.a=10;//ERROR! Cannot access private member
ob2.a=99;//by non-member functions.
cout << ob1.get_a()<<"\n";
cout << ob2.get_a()<<"\n";
return 0;
}
#include <iostream.h>
class myclass{
public:
//now a is public
int a;
// and there is no need for set_a() or get_a()
};
main()
{
myclass ob1, ob2;
//here, a is accessed directly
ob1.a=10;
ob2.a=99;
cout<<ob1.a << "\n";
cout<<ob2.a << "\n";
return 0;
}
#include <iostream.h>
#define SIZE 10
// Declare a stack class for characters
class stack{
char stck[SIZE];//holds the stack
int tos; //index of top-of-stack
public:
void init(); //initialize stack
void push(char ch); //push character on stack
char pop(); //pop character from stack
};
// Initialize the stack
void stack::init()
{
tos=0;
}
//Push a character
void stack::push(char ch)
{
if(tos==SIZE){
cout <<"Stack is full";
return;
}
stck[tos]=ch;
tos++;
}
//Pop a character.
char stack::pop()
{
if(tos==0){
cout <<"Stack is empty";
return 0; //return null on empty stack
}
tos--;
return stck[tos];
}
main()
{
stack s1, s2; // Create two stacks.
int i;
// Initialize the stacks.
s1.init();
s2.init();
s1.push('a');
s2.push('x');
s1.push('b');
s2.push('y');
s1.push('c');
s2.push('z');
for(i=0;i<3;i++) cout << "Pop s1:" << s1.pop() << "\n";
for(i=0;i<3;i++) cout << "Pop s2:" << s2.pop() << "\n";
return 0;
}
Pop s1: c
Pop s1: b
Pop s1: a
Pop s2: z
Pop s2: y
Pop s2: x
Chapter 1 More Examples
#include <iostream.h>
#include <string.h>
class employee{
public:
char name[64];
long employee_id;
float salary;
void show_employee(void)
{
cout << "Name: "<< name << endl;
cout << "Id: "<< employee_id << endl;
cout << "Salary: "<< salary << endl << endl;
};
};
main (void)
{
employee worker, boss;
strcpy(worker.name, "John Doe");
worker.employee_id = 7;
worker.salary = 25000;
strcpy(boss.name, "James Doe");
boss.employee_id = 1;
boss.salary = 55000;
worker.show_employee();
boss.show_employee();
return (0);
}
#include <iostream.h>
#include <string.h>
class dogs{
public:
char breed[64];
int avg_ht;
int avg_wt;
void show_breed(void);
};
void dogs::show_breed(void)
{
cout << "Breed: "<< breed << endl;
cout << "Average Height: "<< avg_ht << endl;
cout << "Average Weight: "<< avg_wt << endl << endl;
}
main (void)
{
dogs happy, matt;
strcpy(happy.breed, "Dalmatian");
happy.avg_ht = 58;
happy.avg_wt = 24;
strcpy(matt.breed, "Sheepdog");
matt.avg_ht = 22;
matt.avg_wt = 15;
happy.show_breed();
matt.show_breed();
return (0);
}