0% found this document useful (0 votes)
10 views18 pages

C++ Lab (Oop)

The document provides multiple examples of C++ programming concepts, particularly focusing on object-oriented programming (OOP) principles such as class definition, member functions, constructors, destructors, and operator overloading. Each example illustrates different aspects of class usage, including public and private access modifiers, member functions defined inside and outside the class, and the use of pointers to objects. Overall, it serves as a comprehensive guide for understanding OOP in C++.

Uploaded by

zcs16083
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

C++ Lab (Oop)

The document provides multiple examples of C++ programming concepts, particularly focusing on object-oriented programming (OOP) principles such as class definition, member functions, constructors, destructors, and operator overloading. Each example illustrates different aspects of class usage, including public and private access modifiers, member functions defined inside and outside the class, and the use of pointers to objects. Overall, it serves as a comprehensive guide for understanding OOP in C++.

Uploaded by

zcs16083
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ Lab (oop)

Defining the Class


#include<iostream>
using namespace std;
class student
{ public:
int age;
string name ;
int grade;
};

int main()
{
student ob1;
student ob2;
student ob3;
[Link]=50;
[Link]="ahmad";
[Link]=30;
cout<<"ob1="<<[Link]<<endl;
cout <<"ob1="<<[Link]<<endl;
cout <<"ob1="<< [Link]<<endl;
[Link]=15;
[Link]="mohammed";
[Link]=99;
cout << "--------------"<<endl;
cout <<"ob2="<<[Link]<<endl;
cout <<"ob2="<<[Link]<<endl;
cout <<"ob2="<<[Link]<<endl;
}

Example 2: Defining the Class


#include <iostream>
using namespace std;
class Book
{
public:
string title;
string author;
int price;
int pages;
};
int main()
{
Book book1;
[Link] = "C++ LEVEL 1";
[Link] = "ALI";
[Link] = 160;
[Link] = 17;
cout << "Book1" << endl;
cout << "Book1 title " << [Link] <<"\n" << endl;
cout << "Book1 author " << [Link] <<"\n"<< endl;
cout << "Book1 price " << [Link] <<"\n"<< endl;
cout << "Book1 pages " << [Link]<<"\n" << endl;}
return 0;
}
Example 3: Member Functions(Using Memeber Function) ,inside the class
#include<iostream>
using namespace std;
class Car
{
public:
string name;
int price;
int model;
// USING MEMEBER FUNCTION
void print()
{ cout << "----------------" << endl;
cout << " Name = " << name << endl;
cout << " Price = " << price << endl;
cout << " Model = " << model << endl; }
};
int main()
{
Car C,C1;
[Link] = "KIAA";
[Link] = 1200;
[Link] = 2023;
[Link] = "TOYOTA";
[Link] = 10000;
[Link] = 2018;
[Link]();
[Link]();
}
Example 4: Private And Public : Using Memeber Function(Inside The Class)
#include<iostream>
using namespace std;
class crectangle
{
private:
int x,y;
public:
void set_values(int a,int b)
{
x=a;
y=b;
}
int area( )
{
return x*y;
}
};
int main( )
{
crectangle rect,rectb;
rect.set_values(3,4);
rectb.set_values(5,6);
cout<<"rect area ="<<[Link]( )<<endl;
cout<<"rectb area ="<<[Link]( )<<endl;
}
Private And Public : Using Memeber Function(Inside The Class
#include<iostream>
using namespace std;
class company
{ private:
string name ;
int id;
int salary;
public:
void add( string n, int i , int sal)
{ name = n;
id=i;
salary=sal;
}
void print()
{
cout<<"your name is : " << name <<endl ;
cout<<"your id is : " << id <<endl ;
cout<<"your salary is : " << salary <<endl ;
}
};
int main ()
{ company ob1;
[Link]( "sss",123,500);
[Link]();
}
Example 5: Private And Public
// Using Member Function(Inside The Class)
#include <iostream>
using namespace std;
class smalljob //define a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{
somedata = d;
}
void showdata() //member function to display data
{
cout << "Data is = "<< somedata << endl;
}
};
int main()
{
smalljob s1, s2; //define two objects of class smallobj
[Link](1066); //call member function to set data
[Link](1776);
[Link](); //call member function to display data
[Link]();
return 0;
}
Example 6:program of using memeber function(Outside the class ), and using cin , scope Resolution Operator

#include<iostream>
using namespace std;
class myclass
{
int a;
public:
void set_a(int);
int get_a( );
};
void myclass::set_a(int num)
{
a=nu
m;
}
int myclass::get_a( )
{
return a;
}
main( )
{
int x,y;
cout<<"enter x \n";
cin>>x;
cout<<"enter y \n";
cin>>y;
myclass ob1,ob2;
ob1.set_a(x);
ob2.set_a(y);
cout<<ob1.get_a( )<<"\n";
cout<<ob2.get_a( )<<"\n";
}
Question 1: Write A Program To Create 2 Object And Class Name is computr (By Using
Member Function Outside the class) With 3 Variables()

Example 7: Write a program for calling data class name is Book


#include<iostream>
using namespace std;
class Book
{
public:
string name;
int no;
void save(string n, int i)
{
name = n;
no = i;
}
void display()
{
cout << "\n" << name << "\n" << no << endl;
}
};
int main()
{
Book B;
[Link]("JAVA", 20);
[Link]();
return 0;
}

Example 8: program of USING MEMEBER FUNCTION(Outside the class) ,end area


#include<iostream>
using namespace std;
class Rect
{
public:
int width;
int length;
int heigth;
int print();
};
int Rect::print()
{ return width*length*heigth;}
int main()
{ Rect op;
[Link] = 2;
[Link] = 3;
[Link] = 5;
cout<<[Link]()<<endl;
return 0;
}

Example 9: program Using pointer to Object

#include<iostream>
using namespace std;
class Rect
{
public:
int width;
int length;
int heigth;
int print();
};
int Rect::print()
{
return width*length*heigth;
}
int main()
{
Rect op;
[Link] = 5;
[Link] = 10;
[Link] = 5;
Rect* ptr1 =&op; //Create a pointer that points to the object
cout<<ptr1->print()<<endl; //-> Accessing the function using
return 0;}
Example 10: program of private , circle area and circle circumference
#include<iostream>
using namespace std;
class circle
{
private :
int r ;
float cir,area;
public :
void get_r( )
{ // ‫نصف القطر‬
cout << "Enter radius :";
cin >> r ;
}
void cal_area( )
{ // ‫المساحة‬
area = r * r * 3.14 ;
}
void cal_cir( )
{ // ‫المحيط‬
cir = 2 * r * 3.14 ;
}
void display( )
{ cout << "area ="<<area<<"\n cir ="<< cir ; }
};
int main ()
{
circle c1 ;
c1.get_r( );
c1.cal_area( );
c1.cal_cir( );
[Link]( ); }
Example 11: Constructor and Destructor Functions, Outside the class
#include<iostream>
using namespace std;
class myclass
{
int a;
public:
myclass( ); // constructor function
void show( );
};
myclass::myclass( )
{
cout<<"constructor function \n";
a=10;
}
void myclass::show( )
{
cout<<a;
}
main( )
{
myclass ob;
[Link]( );
}
Go to Example 14
Example 12: Destructor Outside the class
#include<iostream>
using namespace std;
class x
{
public:
x( );//
~x( );//
};
x::x( )
{
cout<<"constructor is called \n";
}
x::~x( )
{
cout<<"destructor is called \n";
}
main( )
{
x x1,x2;
}
Example 13: Simple Program Of Constructor

#include <iostream>
using namespace std;
class School
{
private:
string name;
int id;
int no;
public:
School() // Default constructor
{
name = "ali";
id = 12;
no = 123;
}
void show() // display function
{ cout << name << endl;
cout << id<<endl;
cout<< no << endl;
}
};
int main()
{
School ob;
[Link]();
}
Question 1: Write A Program To Create 2 constructor And 2 Obact And Class Name is myclass
(By Using 1 Member Function inside the class) With 3 Variables() using parameters inside
constructor
Example 14: using parameters inside constructor
#include<iostream>
using namespace std;
class myclass
{
int a,b;
public:
myclass(int,int);
void show( );
};
myclass::myclass(int x,int y)
{
cout<<"constructor function \n";
a=x;
b=y;
}
void myclass::show( )
{
cout<<a<<"\t"<<b<<"\n";
}
main( )
{
myclass ob(4,7);
[Link]( );
}

Example 15 : using more than one constructors


#include <iostream>
using namespace std;
class Test
{
private:
Int a, b;
public:
Test()// Default Constructor
{
cout << " Hi i am constructor" << endl;
}
Test(int x, int y) // Parameterized Constructors
{
a = x;
b = y;
}
void show()
{
cout << "A = " << a << endl;
cout << "B = " << b << endl;
}
};
int main()
{
Test ob1; // obj ob default cot..
Test ob(1,2); // obj of paramrt…
[Link](); // display of paramet
[Link](); // display of default cons
return 0;
}
Example 16 : program of copy constructor using Cin function
#include <iostream>
using namespace std;
class Student
{
private:
string name;
int age;
public:
// Default constructor
Student() {
name = "";
age = 0;
}
// Parameterized constructor
Student(string n, int a) {
name = n;
age = a;
}
// Copy constructor
Student(const Student& p)
{
name = [Link];
age = [Link];
}
// Function to display the details
void display( ) {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
// Function to accept user input
void input() {
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
}
};
int main()
{
Student p1; // Creating object using default constructor
[Link](); // Taking input from the user
// Creating another object using the copy constructor
Student p2 = p1;
Student p3 =(p2);

// Display the details of both objects


cout << "\nDetails of p1:" << endl;
[Link]();

cout << "\nDetails of p2 (copied from p1):" << endl;


[Link]();

cout << "\nDetails of p3 (copied from p2):" << endl;


[Link]();
return 0;
}
Example 17 : program of destructor

#include <iostream>
using namespace std;
class Sample
{
private:
int a;
int b;
public:
Sample() // constructor
{
a = 15;
b = 10;
}
~Sample() // destructor function
{
cout << " bye" << endl; //to know the program of destructor is executed
}
void show() // print function
{
cout << "A = " << a << endl;
cout << "B = " << b << endl;
}
};
int main()
{
Sample ob;
ob.~Sample(); // calling destructor function
[Link]();
}

Example 18 : Destructor Is Delete


#include <iostream>
using namespace std;
class Sample
{
private:
int *a; // Pointer to dynamically allocated memory
int -*b;
public:
Sample() // constructor
{
a = new int;
b = new int;
*a = 15;
*b = 10;
}
~Sample() // destructor
{ delete a;
delete b;
cout << " bye" << endl; // the program of destructor is excuted
}
void show()
{ cout << "A = " << *a << endl;
cout << "B = " << *b << endl;
}
};
int main()
{ Sample ob;
ob.~Sample(); // calling destructor function
[Link](); // PRINT FUNCTION
// Destructor will automatically be called when obj goes out of scope
}

Example 19: program of overloading(inside the class) using parameter


#include <iostream>
using namespace std;
class school
{
private:
string name;
int level;
int id;
public:
void print(string n, int I) // overloading function 1
{
name = n;
level = I;
cout << " your name is = " <<name<< endl;
cout << "your level is = "<<level<<endl;
}
void print(int i) // overloading function 2
{
id= i;
cout << " your id is = " << id << endl;
}
};
int main()
{
school x;
[Link]("Ahmed",52);
[Link](8);
}

Example 20: Program Of Overloading Outside the Class


#include <iostream>
using namespace std;
class school
{
private:
string name;
int level;
int id;
public:
void print(string n, int i);
void print(int l);
};
void school:: print(string n, int i)
{
name = n;
level = i;
cout << name << endl;
cout << level << endl;
}
void school::print(int l)
{
level = l;
cout << level << endl;
}
int main()
{
school x;
[Link]("ALI", 22);
[Link](1);
}

Example 21:
#include <iostream>
using namespace std;
class school
{
private:
string name;
int level;
int id;
public:
void print(string n, int i);
void print(int l);
};
void school:: print(string n, int i)
{
name = n;
level = i;
cout << name << endl;
cout << level << endl;
}
void school::print(int l)
{
level = l;
cout << level << endl;
}
int main()
{
school x;
[Link]("ALI", 22);
[Link](1);

Example 22: program of Function That Receives Objects


#include <iostream>
using namespace std;
class Dept
{
private:
int id;
int marks;
public:
Dept(int i, int m) // paramtrz constr
{
id = i;
marks = m;
}
void add(Dept ob1, Dept ob2)// function receive objects
{
cout << [Link] + [Link] <<endl; // To Add Two objects
}
};
int main()
{
Dept ob1(1, 50);
Dept ob2(2, 30);
[Link](ob1, ob2); // calling function , (add becz define inside class)
return 0;
}

Example 23: program of Uses of Static Class Data

#include <iostream>
using namespace std;
class f
{
private:
static int counter; // Static member variable to count instances
public:
f() // Constructor
{
counter++; // Increment counter whenever an object is created
}
static int getcounter() // Static member function to get the value of counter
{
return counter; // Return the current value of counter
}
};
int f::counter = 0; // Initialize the static member variable
int main()

{
f a,b, c; // Create first,second and third instance
cout << f::getcounter() << endl; // Call the static member function to print the count of instances
}

Example 24: program using operator --


#include<iostream>
using namespace std;
class beta
{
private:
int b ;
public:
beta()
{
b=1;
}
beta(int) ;
void display( ) ;
beta operator -- ( ) ;
};
beta::beta( int b1 )
{
b = b1 ;
}
beta beta :: operator --( )
{
int b1 ;
b1 = -- this -> b ;
return b1 ;
}
void beta::display( )
{
cout<< b << "\n" ;
}
main( )

{
beta b1 , b2(20);
[Link]( );
[Link]( );
--b1 ;
[Link]( );
--b2;
[Link]( );
beta b3 ;
b3 = --b1 ;
[Link]( ) ;
b3 = --b2 ;
[Link]( ) ;
}

Example 24: Friend Function


#include <iostream>
using namespace std;
class stu
{
private:
int mark;
public:
stu(int m) // constructor receiving data
{
mark = m;
}
void print(); // for display
friend void show(stu ob1); // friend function (to receiving parameters(object)
};
void stu::print() // PRINT FUNCTION
{
cout << "mark is " <<mark << endl;
}
void show(stu ob1) //this is define friend function outside class
{
cout << " your mark is " << [Link] << endl;
}
int main()
{
stu ob1(70);
[Link](); //ob1 it will take everything from class except friend function
show(ob1); // already we have given object of class above
return 0;
}

Example25: Friend Function ,constructor 2 ,inputs from user , 3objects , function sum marks,checkmarks max
#include <iostream>
using namespace std;
class Student
{
private:
string name;
int mark;
public:
Student(string n, int m)
{ name=n;
mark = m;
}
friend void sum(Student ob1,Student ob2, Student ob3);// friend function (to receiving parameters(3object)
void checkmrks(Student ob1,Student ob2, Student ob3) //function (to receiving parameters(3object)
{
if([Link]>[Link] && [Link]>[Link])
{
cout<< [Link]<<" congratulation your are the first \n";
}
else if( [Link]>[Link] && [Link]>[Link])
{
cout<< [Link]<<" congratulation your are the first \n";
}
else if( [Link]>[Link] && [Link]>[Link])
{
cout<< [Link]<<" congratulation your are the first \n";
}
else
{cout <<" all the marks are equal\n";
}
}
};
void sum(Student ob1,Student ob2, Student ob3)
{
int sum=[Link]+[Link]+[Link];
cout<<"sum of the marks "<<sum <<endl;
}
int main()
{ Student ob1("yousf",90);
Student ob2("ali",80);
Student ob3("salh",70);
sum( ob1,ob2, ob3);
[Link](ob1,ob2,ob3);

}
//class cal
// int n1,n2
// default constructo to set the values by cin
// parameterized const to set the values by programmer
// 4 functions
// sum() to add n1,2
// divide() to divide n1,n2 and the result must be in decimal 1 / 2= 0.5
// mod() to modulus n1,n2 %
// mult() to multiply n1,n2
// two object in int main

Example 26: inheritance on constructor


#include<iostream>
using namespace std;
class person{
private:
string name;
public:
void setvalue( string n){
name=n;
}
void print ()
{
cout<<" the name is :"<<name<<endl;
}
};
class student{
private:
int grade;
public:
void setgrade( int a)
{
grade=a;
}
void display(){
cout<<"the grade is :"<<grade <<endl;
}
};
int main(){
person ob1;
[Link]("ali");
[Link]();

student ob2;
[Link](10);
[Link]();
}
//‫عند تعريف الكالس الثاني بالوراثة يمكن استخدام اوبجكت واحد فقط‬
// class student : public person
// int main()
//{ student ob1;
//[Link]("ali");
// [Link]();
// [Link](10);
// [Link](); }
#include<iostream>
using namespace std;
class first
{
private:
int x;
public:
first(int a)
{
x=a;
cout<<"I am constructor of first class"<<endl;
cout<<x<<endl;
}
};
class second : public first
{
private:
int y;
public :
second ( int a, int b):first(a)
{ y=b;
cout << "this is constructor of scend class"<<endl;
cout<<a<<endl;
cout<<b<<endl;
}
};
int main()
{
second ob(10,12);
first ob2(5);
}

Example 28: Single Inheritance


#include<iostream>
using namespace std;
class Person
{
private:
string name;
int age;
string email;
public:
void setValues(string n, int a, string em)
{
name = n;
age = a;
email = em;
}
void print()
{
cout << name << " \n" << age << " \n" << email << endl;
}
};
class student : public Person // derived class(inheritance function)
{
private:
int grade;
public:
void getvalue(int g) //calling function
{
grade = g;
}
void dispaly()
{
cout << "===========" << endl;
cout << grade << " \n" << endl;}
};
int main()
{
student ob1;
[Link](78);
[Link]();
[Link]("ala",100, "hotmail");
[Link]();
}

Eng.

You might also like