Projects Inventory

C++ OOP program  to print stars in a square using classes

C++ OOP program  to print stars in a square using classes.

#include<iostream> 
// header file it provide input output service to c++ programs.
using namespace std;  // namespace 

class a // class is created with " a" name 
{ // class "a " is starting from here 
	protected: // Protected members are accessible within the class and within its derived/child classes.
	int n,i,j; // int is datatype and n,i,j are variables of integer type and this is protected.these are accessible within the class and within its derived/child classes.
	public: // Public members are accessible within the class and also outside the class.
 
 	int out() // out is a function and this is public that can be accessible outside the class.
 	{
 	// out function is started from here 
 		cout<<"enter number"; 
		// this line ask the user to enter number
		
		cin>>n; 
		// this line used to take input from user 

		for(i=1;i<=n;i++) 
		// for loop is started with value of i is equal to 1 and loop will execute until value of i is less than n  and loop will increment if condition is true 
 		{ 
		// started nested loop
 			for( j=1;j<=n;j++) 
			// this loop is started from 1 j is equal to 1 and loop will execute until the value of j is less than n and loop will increment if condition is true 
 			{ 
			// started of nested for loop
 				if(i==1||i==n||j==1||j==n) 
				// in nested loop using if statement in this statement if i is eqaul to 1 or i is   equal to  n ( n that is entered by user)  or j is equal to 1 or n then this condition will true 
 				{ 
				// starting of if condition
 					cout<<"*"; 
					// if,if condition is true then this statement will display * on the that places where if statement is true 
				} 
				// ending of cout statement 
 				else // else condition is started from here 
 				{ 
				// else condition started 
 					cout<<" "; 
					// if , if condition will false then else program move to the else condition and this statement will show space on that places where if statement is not true 
 				}
				// ending of else condition
 			}
			//  ending of if statement
 			
			cout<<endl; // this statement is used to enter space 
 		} 
		// ending of for loop
 	}
	// ending of out function
}; 
// ending of class

int main() // starting main function
// main function is started 
{ 
 	a c; 
	// a is name of class and c is object of class a 
	
	c.out(); 
	// out function is a property of object c and c is a object of class a 
} 
// ending of main function
Advertisement
Advertisement

 

Exit mobile version