Complex A, B, C;
C = A + B;
class ClassName
{
};
class Box
{
double Length, Width, Height;
int Color;
};
class Bicycle
{
char Name[10];
int Color;
int Wheel;
int Gear;
int CurrentGear;
void ChangeGear();
};
class Box
{
double Length ,Width,Height;
int Color;
double Volum();
};
Box MyBox;
include<iostream>
using namespace std;
[color=red]class Box
{
double Length,Width,Height;
int Color;
Double Volum();
};
[/color]
int main()
{
[color=red]
Box MyBox; // Declare Object MyBox [/color]
int i;
float Volum;
. . .
return 1;
}
MyBox.Color جهت دسترسي به رنگ جعبه
MyBox.Length دسترسي به طول جعبه
MyBox.Volum () دسترسي به حجم جعبه
class Box
{
[color=red]public :[/color]
double Volum();
[color=red]private :[/color]
double Length,Width,Height;
};
#include<iostream>
using namespace std;
[color=red]class Box
{
public :
double Volum();
private :
double Length,Width,Height;
};
[/color]
int main()
{
[color=red]
Box MyBox; // Declare Object MyBox
[/color][color=blue]
MyBox.Volum(); // It is OK
MyBox.Length=8; // It is wrong
[/color]
. . .
return 1;
}
class Rectangle {
int x, y;
[color=red] public:[/color]
void set_values (int,int);
int area (void);
} rect;
class Rectangle {
int x, y;
public:
void set_values (int a , int b )
{
x=a; y=b;
}
int area (void)
{
return (x*y);
}
};
#include <iostream.h>
[color=red]class Rectangle {
int x, y;
public:
void set_values (int a , int b )
{
x=a; y=b;
}
int area (void)
{
return (x*y);
}
};[/color]
int main () {
[color=red]Rectangle rect , rectb;[/color]
[color=blue] rect.set_values (3,4);
rectb.set_values (5,6);[/color]
cout << "rect area: " << [color=blue]rect.area()[/color] << endl;
cout << "rectb area: " << [color=blue] rectb.area()[/color] << endl;
}
#include <iostream.h>
[color=red]class Rectangle {
int x, y;
public:
void set_values (int,int);
int area (void) {return (x*y);}
};[/color]
[color=green]void Rectangle[/color][color=red]::[/color][color=green]set_values (int a, int b) {
x = a;
y = b;
}[/color]
int main () {
[color=red]Rectangle rect;[/color]
[color=blue] rect.set_values (3,4);[/color]
cout << "area: " << [color=blue]rect.area()[/color];
}
#include <iostream>
#include <cstring>
using namespace std;
[color=red]class student {
char name[30];
public:
void putname(char *str);
void getname(char *str);
private:
int grade;
public:
void putgrade(int g);
int getgrade();
};
[/color]
[color=green]void student[/color][color=red]:: [/color][color=green]putname(char *str) {
strcpy(name, str);
}
void student[/color][color=red]:: [/color][color=green]getname(char *str) {
strcpy(str, name);
}
void student[/color][color=red]:: [/color][color=green]putgrade(int g) {
grade = g;
}
int student[/color][color=red]:: [/color][color=green]getgrade() {
return grade;
}[/color]
int main() {
[color=red]student x;[/color]
char name[50];
[color=blue]x.putname("GoodBoy");
x.putgrade(5);
x.getname(name); [/color]
cout<<name<<" got "<<[color=blue]x.getgrade()[/color]<<" from OOP."<<endl;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
class student {
char name[30];
public:
void putname(char *str);
void getname(char *str);
private:
int grade;
public:
void putgrade(int g);
int getgrade();
};
void student::putname(char *str) {
strcpy(name, str);
}
void student::getname(char *str) {
strcpy(str, name);
}
void student::putgrade(int g) {
grade = g;
}
int student::getgrade() {
return grade;
}
student x;
x.putname("GoodBoy");
x.putgrade(5);
x.getname(name);
cout<<name<<" got "<<x.getgrade()<<" from OOP."<<endl;
class Rectangle {
int width, height;
public:
Rectangle ();
int area (void) {return (width*height);}
};
Rectangle::Rectangle () {
width = 10;
height = 20;
}
int main () {
Rectangle rect;
cout << "rect area: " << rect.area() << endl;
return 1;
}
#include <iostream.h>
class Vector {
public:
int x,y;
Vector (int,int);
};
Vector::Vector (int a, int b) {
x = a;
y = b;
}
int main () {
[color=red] Vector c (3,1);[/color]
cout << c.x << "," << c.y;
return 0;
}
#include <iostream.h>
class Vector {
public:
int x,y;
Vector ();
Vector (int,int);
};
[color=blue]Vector::Vector (int a, int b) {
x = a;
y = b;
}[/color]
[color=red]Vector::Vector () {
x = 10;
y = 20;
}[/color]
int main () {
Vector a (3,1);
Vector c;
cout << c.x << "," << c.y;
cout << a.x << "," << a.y;
return 0;
}
#include <iostream.h>
class Rectangle {
int *width, *height;
public:
Rectangle (int,int);
~Rectangle ();
int area (void) {return (*width * *height);}
};
Rectangle::Rectangle (int a, int b) {
width = new int;
height = new int;
*width = a;
*height = b;
}
[color=red]Rectangle::~Rectangle () {
delete width;
delete height;
}[/color]
int main () {
Rectangle rect (3,4), rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area (void) {return (width * height);}
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
int main () {
CRectangle [color=blue]a[/color],[color=red] *b, *c;[/color]
CRectangle [color=DarkOrange]* d = new CRectangle[2];[/color]
[color=red]b= new CRectangle;
c= &a;[/color]
[color=blue]a.set_values (1,2);[/color]
[color=red]b->set_values (3,4);
d->set_values (5,6);[/color]
[color=DarkOrange] d[1].set_values (7,8);[/color]
cout << "a area: " <<[color=blue] a.area()[/color] << endl;
cout << "*b area: " << [color=red]b->area()[/color] << endl;
cout << "*c area: " << [color=red]c->area()[/color] << endl;
cout << "d[0] area: " << [color=DarkOrange]d[0].area()[/color] << endl;
cout << "d[1] area: " << [color=DarkOrange]d[1].area()[/color] << endl;
return 0;
}
#include <iostream.h>
class CDummy {
public:
int isitme (CDummy& param);
};
int CDummy::isitme (CDummy& param)
{
if (¶m == [color=red]this[/color]) return 1;
else return 0;
}
int main () {
CDummy a;
CDummy* b = &a;
if ( b->isitme(a) )
cout << "yes, &a is b";
return 0;
}
CVector& CVector::Copy ( CVector& param)
{
x=param.x;
y=param.y;
return *[color=red]this[/color];
}
#include <iostream.h>
class CRectangle {
int width, height;
[color=red] friend CRectangle duplicate (CRectangle);[/color]
public:
void set_values (int, int);
int area (void) {return (width * height);}
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
[color=red]CRectangle duplicate (CRectangle rectparam)
{
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}[/color]
int main () {
CRectangle rect, rectb;
rect.set_values (2,3);
cout << rect.area()<<endl;
[color=red]rectb = duplicate (rect);[/color]
cout << rectb.area()<<endl;
}
#include <iostream.h>
[color=green]class CSquare;[/color]
class CRectangle {
int width, height;
public:
int area (void)
{return (width * height);}
void convert ([color=blue]CSquare a[/color]);
};
[color=green]class Csquare[/color] {
private:
int side;
public:
void set_side (int a)
{side=a;}
[color=red] friend class CRectangle;[/color]
};
void CRectangle::convert (CSquare a) {
[color=red]width = a.side;
height = a.side;[/color]
}
int main () {
CSquare sqr;
CRectangle rect;
sqr.set_side(4);
rect.convert(sqr);
cout << rect.area();
return 0;
}
int a,b,c;
a=b+c;
struct { char product [50]; float price; } a, b, c;
a = b + c;
+ - * / = < > += -= *= /= << >>
<<= >>= == != <= >= ++ -- % & ^ ! |
~ &= ^= |= && || %= [] () new delete
type operator sign (parameters);
#include <iostream.h>
class CVector {
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b) {
x = a;
y = b;
}
CVector CVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main () {
CVector a (3,1);
CVector b (1,2);
CVector c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}
c = a + b;
c = a.operator+ (b);
#ifndef CAR_H
#define CAR_H
class Car {
public:
protected:
};
#endif
// اينكلود كردن كلاس هيدر فايل مربوطه
[color=red]#include "Car.h"[/color]
void f()
{
Car a;
Car b(100, 73);
// ...
}
int main()
{
f();
}
#include "Car.h"
void f()
{
Car a;
a.startEngine();
a.tuneRadioTo("AM", 770);
[color=red]}[/color]
int main()
{
f();
}
#include "Car.h"
void f()
{
Car a;
for (int i = 0; i < 10; ++i) {
Car b;
// ...
[color=red]}[/color] // Each iteration's [color=red]b[/color] dies here
// ...
[color=red]}[/color] // Object [color=red]a[/color] dies here
int main()
{
f();
}